2017-07-20 1 views
1

をキャッチしません:春、私はRestControllerアノテーションを使用している場合、このように、サービスとしてクラスを宣言するために、なぜ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件のステータスを取得し、コンソールで:

私は

@Controller @RequestMapping("/registration")

@RestController("/registration") を変更して、メソッドの宣言の上@ResponseBodyを追加した場合は

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

すべてが動作します。

これは私の設定です:

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

'@RestController( "/登録")'ハンドラを登録しないよう@Requestmappingに必要なので、そのURLで '/ registration'がBeanの* NAME *になりました。マッピングを指定するには、クラスの '@RequestMapping("/registration ")を実行する必要があります。 –

答えて

3

RestControllerの宣言が見えます。だからあなたはそれを追加する必要があります。だから、次のように動作しなければならないURLマッピングを含まない

@RestController 
@RequestMapping("/registration") 
1

正しい使い方は次のとおりです。

@RestController 
@RequestMapping("/registration") 

value@RestControllerがコンポーネント名です。

RestController (Spring Framework 4.3.9.RELEASE API)から:

パブリック抽象文字列値

値が に自動検出コンポーネントの場合のばね豆になっても、論理的なコンポーネント名の提案を示すことができます。それは@Controller@ResponseBodyではなく@RequestMappingが含ま

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

:よう

0

@RestControllerをあなたが同様に

@RestController 
@RequestMapping("/registration") 
関連する問題