创建/更新多个资源的RESTful方式是什么
在服务器上使用Spring MVC,我们有您的基本REST API:创建/更新多个资源的RESTful方式是什么,rest,spring-mvc,Rest,Spring Mvc,在服务器上使用Spring MVC,我们有您的基本REST API: @Controller @RequestMapping(value="/entities") public class EntityController { //GET /entities @RequestMapping(method=RequestMethod.GET) @ResponseBody public List<Entity> getEntities() ..
@Controller
@RequestMapping(value="/entities")
public class EntityController
{
//GET /entities
@RequestMapping(method=RequestMethod.GET)
@ResponseBody
public List<Entity> getEntities()
...
//GET /entities/{id}
@RequestMapping(value="/{id}", method=RequestMethod.GET)
@ResponseBody
public Entity getEntity(@PathVariable Long id)
...
//POST /entities
@RequestMapping(method=RequestMethod.POST, consumes="application/json")
@ResponseBody
public Entity createEntity(@RequestBody Entity entity)
...
//PUT /entities
@RequestMapping(method=RequestMethod.PUT, consumes="application/json")
@ResponseBody
public Entity updateEntity(@RequestBody Entity entity)
...
}
它将具有与updateEntity
相同的URL,但处理列表([…]
)。updateEntity
将处理单个对象({…}
)。但是,在服务器启动时,我出现以下错误:
java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'entityController' bean method public java.util.List<foo.bar.Entity> foo.bar.EntityController.updateEntities(java.util.List<foo.bar.Entity>) to {[/entities],methods=[PUT],params=[],headers=[],consumes=[application/json],produces=[],custom=[]}: There is already 'entityController' bean method public foo.bar.Entity foo.bar.EntityController.updateEntity(foo.bar.Entity) mapped.
java.lang.IllegalStateException:找到不明确的映射。无法将'entityController'bean方法public java.util.List foo.bar.entityController.updateEntities(java.util.List)映射到{[/entities],方法=[PUT],参数=[],头=[],消费=[application/json],生产=[],自定义=[]):已经有'entityController'bean方法public foo.bar.Entity foo.bar.entityController.updateEntity(foo.bar.Entity)映射。
因此,从我收集的信息来看,Spring不喜欢使用相同的@RequestMapping
的两种不同方法,尽管@RequestBody
是不同的
这就引出了两个问题。首先,我是不是用正确的休息方式?当我对同一个URL执行PUT操作并且只允许请求主体成为单个对象或列表时,我是否符合RESTful原则?Spring希望有另一种正确的方法来实现这一点吗?(好的,第一个问题实际上是三个…)
第二个问题是,我是否可以在@RequestMapping
注释中添加一些东西来充分区分这两种方法,但保留相同的restapi
感谢您对此提供的帮助。@JsonIgnoreProperties(ignoreUnknown=true)适用于每种型号
我已经这样做了…有两个模型:用户和树
@RequestMapping(value = "/users/testBean", method = RequestMethod.POST, consumes={"application/json","application/xml"}, produces={"application/json","application/xml"})
public @ResponseBody List<User> testBean(@RequestBody Object object) {
System.out.println("testBean called");
System.out.println(object.toString());
ObjectMapper mapper=new ObjectMapper();
User user =mapper.convertValue(object, User.class);
Tree tree =mapper.convertValue(object, Tree.class);
System.out.println("User:"+user.toString());
System.out.println("Tree:"+tree.toString());
return userService.findAll();
}
@RequestMapping(value=“/users/testBean”,method=RequestMethod.POST,consumes={“application/json”,“application/xml”},products={“application/json”,“application/xml”})
public@ResponseBody列表testBean(@RequestBody对象){
System.out.println(“调用testBean”);
System.out.println(object.toString());
ObjectMapper mapper=新的ObjectMapper();
User=mapper.convertValue(对象,User.class);
Tree-Tree=mapper.convertValue(对象,Tree.class);
System.out.println(“用户:”+User.toString());
System.out.println(“Tree:+Tree.toString());
返回userService.findAll();
}
@RequestMapping(value = "/users/testBean", method = RequestMethod.POST, consumes={"application/json","application/xml"}, produces={"application/json","application/xml"})
public @ResponseBody List<User> testBean(@RequestBody Object object) {
System.out.println("testBean called");
System.out.println(object.toString());
ObjectMapper mapper=new ObjectMapper();
User user =mapper.convertValue(object, User.class);
Tree tree =mapper.convertValue(object, Tree.class);
System.out.println("User:"+user.toString());
System.out.println("Tree:"+tree.toString());
return userService.findAll();
}