我从@RestController返回一个对象获得404响应。 一切似乎都很好,因为我从同一个@RestController中的另一个url得到了正确的响应,但它是一个不同的类。
我发现日志中没有异常或任何其他错误。只有404。
有问题的类定义如下:
public class Menu implements Serializable {
private static final long serialVersionUID=1L;
private String url;
private List<Menu> submenu;
... getters and setters ...
}
我对类似课程没有任何问题。唯一的区别是有一个String列表而不是菜单列表。
可能导致问题的原因是什么?
编辑: 不想在问题中添加太多代码。 这是控制器:
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;
}
}
这是LoginStatus类
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 ...
}
这是在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>
现在,对hostname / menu / app.do的get请求返回404错误代码,而对hostname / menu / test1.do的get请求返回格式正确的json对象。
为了简单起见,我省略了菜单的初始化。
使用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}
答案 0 :(得分:1)
在你的app(...)控制器方法中,我认为HttpResponse不是一个有效的参数。它应该是ServletResponse或HttpServletResponse。您可以在此处参考控制器方法的已接受参数类型列表:http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html