AA_Java
AA_Java

Reputation: 21

Struts to spring migration getting 404 error

/spring/fetchAllUsers URL which am trying

web.xml
  <servlet>
            <servlet-name>user</servlet-name>
            <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>
            <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springContext/dataSource.xml</param-value>
        </init-param>
            <load-on-startup>3</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>user</servlet-name>
            <url-pattern>/spring/</url-pattern>
        </servlet-mapping>

        Controller Code
@RequestMapping(value = "/getAllUsers", method = RequestMethod.POST)
@ResponseBody
@Transactional(propagation=Propogation.REQUIRED,rollBackFor=Exception.class)
public String fetchAllUsers(){
setInputStream(userDelegate.fetchAllUsers());
return success;

Details:
And I have mvc annotation driven and mvc default servlet handler in user-servlet.xml

    Getting 404 error code when try to access this URl when doing migration from struts to spring

Break point is not hit when this URL is hit and no errors in console as well.Please suggest on the above issue

Upvotes: 0

Views: 209

Answers (3)

Spring interprets urls /spring/createUser/ and /spring/createUser differently(atleast in POST methods that i just tested).

Change your @RequestMapping url to /spring/createUser.

Also mind that the url you call is /spring/createUser without a trailing slash("/").

Your method

@RequestMapping(value = "/spring/createUser", method = RequestMethod.POST)

Hope this helps.

Upvotes: 0

Mohd Yasin
Mohd Yasin

Reputation: 483

According to your servlet mapping only one url is allowed localhost:8080/context/spring/ that is not mapped with your controller.

When we defined a servlet-mapping, we are using SimpleUrlHandlerMapping. To understand servlet url mapping let define a servlet mapping :

<servlet-mapping>
        <servlet-name>user</servlet-name>
        <url-pattern>/spring/*</url-pattern>
</servlet-mapping>

Now the handler will actually use the * part to find the controller. It will not search /spring/createUser, it will only search for /createUser to find a corresponding Controller.

@RequestMapping("/createUser")

In your case You need to either change your url to localhost:8080/spring/spring/createUser or remove prefix from Controller @RequestingMapping(/createUser).

Upvotes: 0

Chi Dov
Chi Dov

Reputation: 1527

since your servlet url mapping has already include /spring you don't need to include it in @RequestMapping.

try this:

@RequestMapping(value = "/createUser/", method = RequestMethod.POST)

Upvotes: 0

Related Questions