I have a web app which has JSPs as a view. But now I am trying to implement a RESTful in the same web app. I created a new controller, where will be my RESTful(I am not sure if it makes sense yet).
I have found some examples by internet and I have been trying to make my RESTful following these examples.
When I access my RESTful URL it return me 404 Not Found. I took a look at in some questions similars here in stackoverflow and tried to change some XML configurations, but it does not works, so far.
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.
I think, maybe I need to set up the XML to be compatible with both kinds, RESTful and a common web app, but I am not sure, and I do not know exactly how to do it.
Thanks and follows my codes:
My 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>
spring-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/**" />
Controller
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;
}
The problem is your SuggestionController should have a @Controller annotation on it so Spring will treat it as a Controller (it should also be in the com.restfultest package).
Now I'm not sure if this matters butin the spring-context.xml where you have
<context:component-scan base-package="com.restfultest." />
I've never seen it with a dot at the end. So you might consider removing it, but it might not make a difference.