简体   繁体   中英

Getting 404 response on spring 4 rest service

I am getting a 404 response from a @RestController returning an object. Everything seems fine as I get a correct response from another url in the same @RestController, but it is a different class.

I see no exception or any other error in the logs though. Only 404.

Problematic class is defines as follows:

public class Menu implements Serializable {
private static final long serialVersionUID=1L;
private String url;
private List<Menu> submenu;
... getters and setters ...
}

I have no problems with a similar class. Only difference is there is a List of String instead of a List of Menu.

What may be causing the problem?

EDIT: Didn't want to add too much code to the question. This is the Controller:

package org.web.ui.controller;

import java.io.IOException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Collection;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;

import mx.org.ife.rfe.siirfe.comun.web.bean.LoginStatus;
import mx.org.ife.rfe.siirfe.comun.web.controller.UserLoginController;
import mx.org.ife.rfe.siirfe.comun.web.ui.model.Menu;

@RestController
@RequestMapping("/menu")
public class menuController {

private LoginStatus loginStatus ;

public menuController() {
    ArrayList<String> _roles = new ArrayList<>();
    _roles.add("TESTCASE");
    loginStatus = new LoginStatus();
    loginStatus.setRoles(_roles);

    loginStatus.setAnonymous(true);
    loginStatus.setError(false);


}

@RequestMapping(value = "/app.do")
public Menu app(HttpResponse response) {

    return new Menu();
}

@RequestMapping(value="/test1.do")
public LoginStatus test1(Principal user) {
    return loginStatus;
}


}

This is LoginStatus class

public class LoginStatus implements Serializable {

private static final long serialVersionUID = 1L;
private boolean anonymous = true;
private Boolean error;
private String errorMessage;
private List<String> roles;
... Getters and Setters ...
}

This is in the web.xml

<servlet>
    <servlet-name>spring-dispatcher-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:/webApplicationContext.xml,
            classpath*:/environmentContext.xml,
            classpath*:/daoContext.xml,
            classpath*:/menuContext.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring-dispatcher-servlet</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

Now, a get request to hostname/menu/app.do returns a 404 error code while a get request to hostname/menu/test1.do returns a correctly formatted json object.

I ommited initialization of the menu for simplicity.

Example outputs from test with wget:

$ wget  wlcap1:9102/siilnere-web/menu/app.do -O -
--2016-12-08 18:16:51--  http://wlcap1:9102/siilnere-web/menu/app.do
Resolving wlcap1... 172.19.94.15
Connecting to wlcap1|172.19.94.15|:9102... connected.
HTTP request sent, awaiting response... 404 Not Found
2016-12-08 18:16:51 ERROR 404: Not Found.

06:16 PM
$ wget wlcap1:9102/siilnere-web/menu/test1.do -O -
--2016-12-08 18:16:55--  http://wlcap1:9102/siilnere-web/menu/test1.do
Resolving wlcap1... 172.19.94.15
Connecting to wlcap1|172.19.94.15|:9102... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/json]
Saving to: 'STDOUT'
2016-12-08 18:16:55 (7.62 MB/s) - written to stdout [101]
06:16 PM
$ wget wlcap1:9102/siilnere-web/menu/test1.do -O - 2>/dev/null
{"roles":["EJEMPLO"],"token":null,"anonymous":true,"error":false,"errorMessage":null,"username":null}

In your app(...) controller method, I think that HttpResponse is not a valid param. It should be ServletResponse or HttpServletResponse. You can refer to the list of accepted param types of a controller method here: http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html

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