2

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.

Doo Dah
  • 3,979
  • 13
  • 55
  • 74
  • is this form data supposed to be passed as requestHeaders of should it be modelAttribute? see http://stackoverflow.com/a/20515220/2310289 – Scary Wombat Jan 05 '17 at 02:47
  • Yes. They are suppose to be passed as headers. This is a RESTful API cll and not called from an HTML form. Adding the header params makes no difference to the 404 that is returned. – Doo Dah Jan 05 '17 at 02:48
  • Can you map the whole url in method like @RequestMapping(method = RequestMethod.GET, value = "/rest/message/testNoParams") and remove it from class level. – Tharsan Sivakumar Jan 05 '17 at 04:04
  • Where is your deployed app name in url? Usually :\\ in tomcat. I am not familiar with WildFly. – Valath Jan 05 '17 at 05:08
  • Please provide your web.xml or webapp java configuration. Also, as @Valath said, which is your app name context path? If you see WildFly splash screen while going to http://localhost:8080, your app should be running in a non-root application context – jlumietu Jan 05 '17 at 08:20
  • I added my web.xml – Doo Dah Jan 05 '17 at 12:15

2 Answers2

3

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 (/../), i.e. WAR has been deployed with some other name and may be you are hitting the incorrect path.

This often occurs with wildfly deployment.

e.g. : 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.

  • This can be done programmatically as found in this link http://stackoverflow.com/questions/32241906/how-to-deploy-war-file-in-root-context-to-wildfly-ver-9-0-1 by adding a jboss-web.xml to the WEB-INF folder with the following content: /appName – Doo Dah Jan 06 '17 at 02:29
1

I had the same issue and fixed it by changing the Class annotation from @Controller to @RestController

T04435
  • 12,507
  • 5
  • 54
  • 54