我有一个以 JSP 作为视图的 Web 应用程序。但现在我正在尝试在同一个 Web 应用程序中实现 RESTful。我创建了一个新控制器,我的 RESTful 将在哪里(我不确定它是否有意义)。
我在互联网上找到了一些示例,并且我一直在尝试按照这些示例制作我的 RESTful。
当我访问我的 RESTful URL 时,它返回给我404 Not Found。我在stackoverflow中查看了一些类似的问题,并尝试更改一些XML配置,但到目前为止它不起作用。
当我访问: 网址:localhost:8080/restfultest/rest/suggestions/11 返回我:404。 当我访问: 网址:localhost:8080/restfultest/loginForm 向我返回正确的 JSP 页面。
我想,也许我需要设置 XML 以兼容这两种类型,RESTful 和一个常见的 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>
弹簧上下文.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;
}