gpt4 book ai didi

java - Spring - RestController 注释没有捕获请求

转载 作者:行者123 更新时间:2023-11-30 06:05:59 25 4
gpt4 key购买 nike

我不明白为什么如果我使用 RestController 注释将类声明为服务,如下所示:

@RestController("/registration")
public class RegistrationService {

@RequestMapping(value="/",
produces="application/json")
public String initializeSession(Model model){

return "{\"success\":1}";
}
}

当我发出像

这样的请求时

http://localhost:8080/SpringRest/registration/

我得到 404 状态并在控制台中:

No mapping found for HTTP request with URI [/SpringRest/registration/] in DispatcherServlet with name 'dispatcherServlet'

如果我更改 @RestController("/registration") 一切正常与

@Controller
@RequestMapping("/注册")

并在方法声明上方添加@ResponseBody。

这是我的配置:

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<display-name>SpringRest</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-config.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<!--
- Servlet that dispatches request to registered handlers (Controller implementations).
-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

调度员

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


<mvc:annotation-driven />

<!-- Uncomment and your base-package here: -->
<context:component-scan
base-package="it.reply.rest"/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>

</beans>

最佳答案

RestController 的声明如下:

    @Target(value=TYPE)
@Retention(value=RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController

它包括@Controller@ResponseBody 但不包括@RequestMapping。所以你需要添加它。所以以下将起作用:

@RestController
@RequestMapping("/registration")

关于java - Spring - RestController 注释没有捕获请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45210005/

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