2013-04-19 71 views
0

我有一個Web應用程序,它具有JSP作爲視圖。但是現在我正試圖在同一個Web應用中實現一個RESTful。我創建了一個新的控制器,其中將是我的RESTful(我不確定它是否合理)。Spring MVC RESTful多視圖 - 404 Not Found

我發現了一些互聯網的例子,我一直試圖讓我的RESTful遵循這些例子。

當我訪問我的RESTful URL時,它返回我404 Not Found。我在stackoverflow中看到了一些類似的問題,試圖改變一些XML配置,但到目前爲止它並不起作用。

 
When I access: 
URL: localhost:8080/restfultest/rest/suggestions/11 
returns me: 404. 

When I access: 
URL: localhost:8080/restfultest/loginForm 
Returns me the correct JSP page. 

我想,也許我需要建立XML要與這兩種,REST風格和常見的Web應用程序兼容,但我不知道,我不知道到底該怎麼做。

感謝,並遵循我的代碼:

我的web.xml

<servlet> 

    <servlet-name>springmvc</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 

    <init-param> 

     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring-context.xml</param-value> 

    </init-param> 

    <load-on-startup>1</load-on-startup> 

</servlet> 

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

彈簧的context.xml

<context:component-scan base-package="com.restfultest." /> 

    <mvc:annotation-driven /> 

    <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"> 
     <property name="contentType" value="application/json"/> 
    </bean> 

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
     <property name="messageConverters"> 
      <util:list id="beanList"> 
       <ref bean="jsonMessageConverter"/> 
      </util:list> 
     </property> 
    </bean> 

    <bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/views/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 


    <mvc:interceptors> 

     <bean class="com.restfultest.interceptor.AutorizadorInterceptor"></bean> 
     <bean class="com.restfultest.interceptor.ConnectionInterceptor"></bean> 

    </mvc:interceptors> 

<mvc:resources location="/resources/" mapping="/resources/**" /> 

控制器

public class SuggestionController { 

    @Autowired 
    private SuggestionService suggestionService; 

    @Autowired 
    private View view; 

    private static final String DATA_FIELD = "data"; 
    private static final String ERROR_FIELD = "error"; 

    private static final String CONTENT_RANGE_HEADER = "Content-Range"; 
    private static final String ACCEPT_JSON = "Accept=application/json"; 

    @RequestMapping(value = "/rest/suggestions/{suggestionId}", method = RequestMethod.GET, headers=ACCEPT_JSON) 
    public ModelAndView getSuggestion(@PathVariable("suggestionId") String suggestionId) { 

     Suggestion suggestion = null; 

     if (isEmpty(suggestionId) || suggestionId.length() < 5) { 
      String sMessage = "Error invoking getSuggestion - Invalid suggestion Id parameter"; 
      return createErrorResponse(sMessage); 
     } 

     try { 

      suggestion = suggestionService.getSuggestionById(suggestionId); 

     } catch (Exception e) { 
      String sMessage = "Error invoking getSuggestion. [%1$s]"; 
      return createErrorResponse(String.format(sMessage, e.toString())); 
     } 

     return new ModelAndView(view, DATA_FIELD, suggestion); 
    } 

    private ModelAndView createErrorResponse(String sMessage) { 
     return new ModelAndView(view, ERROR_FIELD, sMessage); 
    } 

    public static boolean isEmpty(String s_p) { 
     return (null == s_p) || s_p.trim().length() == 0; 
    } 

回答

2

的問題是你的SuggestionController應該有一個@Controller註解,所以Spring將它視爲一個Controller(它也應該在com.restfultest包中)。

現在,我不知道這是否事項戰利品彈簧context.xml中,你必須

<context:component-scan base-package="com.restfultest." /> 

我從來沒有跟在最後一個點看到它。所以你可能會考慮刪除它,但它可能沒有什麼區別。