简体   繁体   中英

Spring MVC Route Not found - 404

I have a Spring 4.1.0 MVC project hosted inside a WildFly 10 server. and I am having trouble sending a request to without getting a 404. Here is my controller and one of the method declarations:

@Controller
@RequestMapping("/rest/message")
public class HomeController {
    @RequestMapping(method = RequestMethod.POST, value = "/register")
    public @ResponseBody PostResult register(HttpServletRequest request, @RequestHeader("userUid") String userUid,
        @RequestHeader("Uid") String Uid, @RequestHeader(value = "firstName", required = false) String FirstName,
        @RequestHeader(value = "lastName", required = false) String LastName,
        @RequestHeader(value = "emailAddress", required = false) String EmailAddress,
        @RequestHeader(value = "userName", required = false) String UserName,
        @RequestHeader(value = "country", required = false) String Country,
        @RequestHeader(value = "password", required = false, defaultValue = "") String Password,
        @RequestHeader(value = "deviceId", required = false, defaultValue = "") String deviceID) {
}

}

I am using Postman to POST a request to http://localhost:8080/rest/message/register . I get back "404 - Not Found". I can successfully navigate to http://localhost:8080 to see the WildFly splash screen. I can also set a breakpoint in the Controller's constructor to see it constructed. In addition, I see the following in the server log leading me to believe the route has actually been registered:

INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/rest/message/register],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public com.justPick5.jp5.returnObjects.PostResult com.justPick5.jp5.HomeController.register(javax.servlet.http.HttpServletRequest,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)

To rule out the possibility of the parameters being the issue, here is a simple method in the controller:

@RequestMapping(method = RequestMethod.GET, value = "/testNoParams")
public @ResponseBody PostResult testNoParams() {
    System.out.println("testNoParams");
    return null;
}

Doing a GET from Postman to http://localhost:8080/rest/message/testNoParams has the same result.

Here is my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </context-param>


    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>

        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</web-app>

Any help appreciated.

I guess this issue is related to context path. http://localhost:8080/ .. /rest/message/testNoParams

If I am not wrong you need to place the correct path over (/ .. /), ie WAR has been deployed with some other name and may be you are hitting the incorrect path.

This often occurs with wildfly deployment.

eg : WAR deployed is ABC.1.0.0-BUILD-SNAPSHOT.war, but we often hit /ABC/ in our path.

So, please correct the context path. You can do this at wildfly -> deployment -> deployment Location.

我有同样的问题,并通过将Class注释从@Controller更改为@RestController@RestController

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM