rest api guide fails 404 not found or tomcat server fails

Question

Using the spring rest API guides both with spring boot and a simple MVC type application, results in 404 not found for the simple GET. I have searched responses from incorrect web.xml contents to servlet-context.xml entries, I am unable to find the source of the problem.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:web="http://java.sun.com/xml/ns/javaee" 
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" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WebContent/WEB-INF/spring-servlet.xml</param-value>
</context-param>
<listener>
<listener-
class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>
    org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>

<servlet-mapping>
<servlet-name>api</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>defaultHtmlEscape</param-name>
<param-value>true</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
              org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
 <filter-name>springSecurityFilterChain</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>
 </web-app>

context.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"
xsi:schemaLocation="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
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

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

</beans>

controller file

@Controller
@RequestMapping("/")
public class UserController {

  @RequestMapping(value = "/greeting", method = RequestMethod.GET)
    public @ResponseBody Greeting greeting(@RequestParam(value="name", 
     defaultValue="World") String name, HttpServletResponse httpResponse_p, 
                                            WebRequest request_p) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));
    }

    }

first the tomcat server fails to start. If I delete the server and recreate, restart then the server starts fine. Then I try and run the application and the server does not start.

Closing the application and reopening did work once but then the 404 appears.


Show source
| rest   | spring   2017-08-08 22:08 1 Answers

Answers to rest api guide fails 404 not found or tomcat server fails ( 1 )

  1. 2017-08-08 22:08

    you have an additional '/' , just remove it from @RequestMapping in the class level

    @Controller
    @RequestMapping("")
    public class UserController {
    

Leave a reply to - rest api guide fails 404 not found or tomcat server fails

◀ Go back