Reputation: 4984
I want to use a REST call in Spring MVC on my webapp and return a JSON, I've tried following mkyong's tutorial on this.http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/
However it isn't working, when I call
http://localhost:8080/openRepairsClient/[email protected]
I get a 404 error.
This is my code:
@Controller
public class OpenRepairsRest {
@Autowired
private RepairService repairService;
@Autowired
private UserService userService;
@RequestMapping(value = "/openRepairsClient/{username}")
public @ResponseBody
List<Repair> openRepairsInJson(@PathVariable("username") String username) {
List<Repair> openRepairs = null;
try {
openRepairs = repairService.findOpenRepairsByClient((Client) userService.getUser(username));
} catch (UserServiceException ex) {
Logger.getLogger(OpenRepairsRest.class.getName()).log(Level.SEVERE, null, ex);
}
return openRepairs;
}
}
And yes I'm sure the username exists, otherwise I would get a userservice exception.
My web.xml file:
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<!-- welcome files -->
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<!-- context parameters -->
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/jsf/faces-config.xml</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>facelets.LIBRARIES</param-name>
<param-value>/WEB-INF/mytags.taglib.xml</param-value>
</context-param>
<!-- listeners -->
<listener>
<!-- Required for Faces to kick in -->
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- Load applicationContext in ServletContextListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Frontcontrollers -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/dispatcher-servlet.xml
</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<filter>
<filter-name>Authentication Filter</filter-name>
<filter-class>be.kdg.repaircafe.filters.AuthenticationFilter</filter-class>
</filter>
<!-- Mappings -->
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>be.kdg.repaircafe.servlets.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<filter-mapping>
<filter-name>Authentication Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/LogoutServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/errorpages/error.jsp</location>
</error-pag
e>
Upvotes: 0
Views: 327
Reputation: 600
Your error is in the url, it appears that you forgot to write the project name and the word rest. Try with this url:
http://localhost:8080/SpringMVC/rest/openRepairsClient/[email protected]
You can try it with this simplified version of your code:
@Controller
public class OpenRepairsRest {
@RequestMapping(value = "/openRepairsClient/{username}")
public @ResponseBody
List<Shop> openRepairsInJson(@PathVariable("username") String username) {
Shop exampleShop = new Shop();
exampleShop.setName("exampleName");
exampleShop.setStaffName(new String[] { username });
List<Shop> openShops = new ArrayList<Shop>();
openShops.add(exampleShop);
return openShops;
}
}
Upvotes: 1
Reputation: 11
You need to put your app name in url, example: http://localhost:8080/MyApp/openRepairsClient/[email protected]
Upvotes: 0