Java Spring REST控制器RestMapping层次结构
Java Spring REST控制器RestMapping层次结构,java,spring,Java,Spring,我有一个REST应用程序及其工作状态。 现在,我正在尝试添加一个超级路径(环境),如下所示: @RestController @RequestMapping("/environments") public class EnvironmetController { @RequestMapping(method = RequestMethod.GET) public List<Environment> getList() { return Environ
我有一个REST应用程序及其工作状态。
现在,我正在尝试添加一个超级路径(环境),如下所示:
@RestController
@RequestMapping("/environments")
public class EnvironmetController {
@RequestMapping(method = RequestMethod.GET)
public List<Environment> getList() {
return EnvironmentDAO.getInstance().getList();
}
@RequestMapping("/{envId}/entityList1")
public EntityList1Controller getEntityList1(@PathVariable int envId) {
return new EntityList1Controller(envId);
}
@RestController
public class EntityList1Controller {
private int envId;
public EntityList1Controller() {
}
public EntityList1Controller(int envId) {
this.envId = envId
}
@RequestMapping(method = RequestMethod.GET)
public List<Entity1> getList() {
return Entity1DAO.getInstance().getList(envId);
}
}
实际路径:所需的新路径:{envId}/entityList1/ 我试图像这样“向前”控制器:
@RestController
@RequestMapping("/environments")
public class EnvironmetController {
@RequestMapping(method = RequestMethod.GET)
public List<Environment> getList() {
return EnvironmentDAO.getInstance().getList();
}
@RequestMapping("/{envId}/entityList1")
public EntityList1Controller getEntityList1(@PathVariable int envId) {
return new EntityList1Controller(envId);
}
@RestController
public class EntityList1Controller {
private int envId;
public EntityList1Controller() {
}
public EntityList1Controller(int envId) {
this.envId = envId
}
@RequestMapping(method = RequestMethod.GET)
public List<Entity1> getList() {
return Entity1DAO.getInstance().getList(envId);
}
}
@RestController
@请求映射(“/environments”)
公共类环境控制器{
@RequestMapping(method=RequestMethod.GET)
公共列表getList(){
返回EnvironmentDAO.getInstance().getList();
}
@请求映射(“/{envId}/entityList1”)
公共EntityList1控制器getEntityList1(@PathVariable int envId){
返回新的EntityList1Controller(envId);
}
EntityList1控制器如下所示:
@RestController
@RequestMapping("/environments")
public class EnvironmetController {
@RequestMapping(method = RequestMethod.GET)
public List<Environment> getList() {
return EnvironmentDAO.getInstance().getList();
}
@RequestMapping("/{envId}/entityList1")
public EntityList1Controller getEntityList1(@PathVariable int envId) {
return new EntityList1Controller(envId);
}
@RestController
public class EntityList1Controller {
private int envId;
public EntityList1Controller() {
}
public EntityList1Controller(int envId) {
this.envId = envId
}
@RequestMapping(method = RequestMethod.GET)
public List<Entity1> getList() {
return Entity1DAO.getInstance().getList(envId);
}
}
@RestController
公共类实体列表1控制器{
私营企业;
公共实体列表1控制器(){
}
公共实体列表1控制器(int envId){
this.envId=envId
}
@RequestMapping(method=RequestMethod.GET)
公共列表getList(){
返回Entity1DAO.getInstance().getList(envId);
}
}
它不起作用。它使用GET方法抛出一个模糊错误
我没有发现我是否可以做这种“控制器转发”,或者我必须配置其他东西
任何帮助都将不胜感激
Thx提前有一些问题 我不知道我是否能做这种“控制器转发” 在EnvironmetController中,您将返回EntityList控制器的一个新实例。您需要像这样对servletResponse应用重定向,但还需要envId(使其像REST一样) 它使用GET方法抛出一个模糊错误 确保EntityList1Controller上有正确的注释,听起来好像两个GET处理程序映射到了同一个端点
@RestController
@RequestMapping("/enitytList1")
public class EntityList1Controller {
您的处理程序应该具有更正注释
@RequestMapping(value="/{envId}",method = RequestMethod.GET)
public List<Entity1> getList(@PathVariable ("envId") Integer envId) {
return Entity1DAO.getInstance().getList(envId);
}
@RequestMapping(value=“/{envId}”,method=RequestMethod.GET)
公共列表getList(@PathVariable(“envId”)整数envId){
返回Entity1DAO.getInstance().getList(envId);
}
这将把来自myApp/environments/{envId}/entityList1/的请求转发到myApp/enitytList1/{envId},这与您想要的非常接近,但您需要请求中的envId
在rest中使用重定向是不寻常的,但希望这能为您指明正确的方向
编辑并且不要直接调用控制器上的构造函数,让Spring为您制作单例