Learner Learner - 2 months ago 25
reST (reStructuredText) Question

Issue in running Spring Restful web services without Spring Boot

I am trying to build Restful web services. My Maven project name is rest and I am following Spring's Building a RESTful Web Service to do that but I don't want to use Spring Boot but just create a war and host it on Tomcat in my eclise/STS. Here are my web.xml and XX-servlet.xml files:

web.xml

<web-app>
<display-name>Archetype Created Web Application</display-name>

<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>


rest-servlet.xml

<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.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-4.0.xsd">

<context:annotation-config />
<context:component-scan base-package="com.rest" />
<mvc:annotation-driven/>


</beans>


GreetingController.java

@RestController
public class GreetingController2 {
private final AtomicLong counter = new AtomicLong();

@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format("Hello %s", name));
}
}


when I run this on Tomcat the URL: http://localhost:8080/rest gives 404 and I see this message in tomcat console
Aug 16, 2017 3:59:28 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/rest/] in DispatcherServlet with name 'rest'
whereas I do have the mapping.

And when I hit http://localhost:8080/rest/greeting I get http 406 with message
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
whereas as per the tutorial it should be converted to JSON which can be rendered in the browser.

I spent a lot of time trying to figure out and looking at various posts on SO to find what's wrong but could not.

Answer Source

You dont have a default mapping like
@RequestMapping("/")
As per your code above you should have below url working fine
http://localhost:8080/rest/greeting