【问题标题】:What is the RESTful way of creating/updating multiple resources什么是创建/更新多个资源的 RESTful 方式
【发布时间】:2013-01-31 05:39:25
【问题描述】:

在服务器上使用 Spring MVC,我们拥有您的基本 REST API:

@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)
    ...
}

这一切都很好。现在我希望能够通过一个请求创建或更新多个Entitys。我的第一个想法是添加这个:

@RequestMapping(method=RequestMethod.PUT, consumes="application/json")
@ResponseBody
public List<Entity> updateEntities(@RequestBody List<T> entities)

它将与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.

因此,据我所知,Spring 不喜欢使用相同 @RequestMapping 的两种不同方法,即使 @RequestBody 不同。

这让我想到了两个问题。首先,我是否以正确的 RESTful 方式进行处理?当我对同一个 URL 执行 PUT 并且只允许请求正文是单个对象或列表时,我是否符合 RESTful 原则? Spring想要另一种正确的方法吗? (好吧,所以第一个问题实际上是三个......)

第二个问题是我是否可以在 @RequestMapping 注释中添加一些内容,以充分区分这两种方法,但保持相同的 REST API?

感谢您对此提供的任何启发。

【问题讨论】:

    标签: rest spring-mvc


    【解决方案1】:

    每个模型的@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();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-21
      • 2010-09-29
      • 2023-03-12
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多