gpt4 book ai didi

java - Spring-MVC 中的 REST 调用

转载 作者:行者123 更新时间:2023-12-01 12:42:11 25 4
gpt4 key购买 nike

我想在我的 web 应用程序上的 Spring MVC 中使用 REST 调用并返回 JSON,我已经尝试按照 mkyong 的教程进行操作。 http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/

但是当我打电话时它不起作用

http://localhost:8080/openRepairsClient/jan.peeters@student.kdg.be

我收到 404 错误。

这是我的代码:

@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;
}
}

是的,我确定用户名存在,否则我会收到用户服务异常。

我的 web.xml 文件:

<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>

最佳答案

您的错误出现在网址中,看来您忘记了项目名称和单词rest。尝试使用此网址:

http://localhost:8080/SpringMVC/rest/openRepairsClient/jan.peeters@student.kdg.be

您可以使用此简化版本的代码进行尝试:

@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;
}
}

关于java - Spring-MVC 中的 REST 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24993357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com