Spring - RestController annotation does not catch

2019-05-24 05:47发布

I don't understand why If I use RestController annotation to declare a class as service, like this:

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

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

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

when I do a request like

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

I get an 404 status and in the console:

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

Everything works If I change @RestController("/registration") with

@Controller @RequestMapping("/registration")

and add @ResponseBody above method declaration.

This is my configuration:

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>

dispatcher

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

3条回答
Emotional °昔
2楼-- · 2019-05-24 06:22

The correct usage is:

@RestController
@RequestMapping("/registration")

The value of @RestController is the component name.

From RestController (Spring Framework 4.3.9.RELEASE API):

public abstract String value

The value may indicate a suggestion for a logical component name, to be turned into a Spring bean in case of an autodetected component.

查看更多
beautiful°
3楼-- · 2019-05-24 06:28

The declaration of RestController looks like:

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

It includes @Controller and @ResponseBody but not @RequestMapping. So you need to add it. So the following shall work:

@RestController
@RequestMapping("/registration")
查看更多
Juvenile、少年°
4楼-- · 2019-05-24 06:38

@RestController not including url mapping so you need to @Requestmapping as well like

@RestController
@RequestMapping("/registration")
查看更多
登录 后发表回答