具有Spring Data Rest功能的自定义Spring MVC HTTP补丁请求 [英] Custom Spring MVC HTTP Patch requests with Spring Data Rest functionality

查看:108
本文介绍了具有Spring Data Rest功能的自定义Spring MVC HTTP补丁请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在自定义Spring MVC控制器中支持HTTP PATCH的最佳实践是什么?特别是在使用HATEOAS/HAL时?是否有一种更简单的方法来合并对象,而不必检查请求json中每个字段的存在(或编写和维护DTO),最好是自动解组资源链接?

What is the best practice for supporting HTTP PATCH in custom Spring MVC controllers? Particularly when using HATEOAS/HAL? Is there an easier way to merge objects without having to check for the presence of every single field in the request json (or writing and maintaining DTOs), ideally with automatic unmarshalling of links to resources?

我知道Spring Data Rest中存在此功能,但是是否可以利用此功能在定制控制器中使用?

I know this functionality exists in Spring Data Rest, but is it possible to leverage this for use in custom controllers?

推荐答案

我认为您不能在此处使用spring-data-rest功能.

I do not think you can use the spring-data-rest functionality here.

spring-data-rest在内部使用 json-patch 库.基本上,我认为工作流程如下:

spring-data-rest is using json-patch library internally. Basically I think the workflow would be as follows:

  • 阅读您的实体
  • 使用objectMapper将其转换为json
  • 应用补丁(这里需要json-patch)(我认为您的控制器应将JsonPatchOperation列表作为输入)
  • 将修补的json合并到您的实体中

我认为最困难的部分是第四点.但是,如果您不必具有通用的解决方案,则可能会更容易.

I think the hard part is the fourth point. But if you do not have to have a generic solution it could be easier.

如果您想了解spring-data-rest的功能,请查看org.springframework.data.rest.webmvc.config.JsonPatchHandler

If you want to get an impression of what spring-data-rest does - look at org.springframework.data.rest.webmvc.config.JsonPatchHandler

编辑

在最新数据中,spring-data-rest中的补丁机制发生了显着变化.最重要的是,它不再使用json-patch库,现在从头开始实现json补丁支持.

The patch mechanism in spring-data-rest changed significantly in the latest realeases. Most importantly it is no longer using the json-patch library and is now implementing json patch support from scratch.

我可以设法在自定义控制器方法中重用主要补丁功能.

I could manage to reuse the main patch functionality in a custom controller method.

以下代码段说明了基于spring-data-rest 2.6的方法

The following snippet illustrates the approach based on spring-data-rest 2.6

        import org.springframework.data.rest.webmvc.IncomingRequest;
        import org.springframework.data.rest.webmvc.json.patch.JsonPatchPatchConverter;
        import org.springframework.data.rest.webmvc.json.patch.Patch;

        //...
        private final ObjectMapper objectMapper;
        //...

        @PatchMapping(consumes = "application/json-patch+json")
        public ResponseEntity<Void> patch(ServletServerHttpRequest request) {
          MyEntity entityToPatch = someRepository.findOne(id)//retrieve current state of your entity/object to patch

          Patch patch = convertRequestToPatch(request);
          patch.apply(entityToPatch, MyEntity.class);

          someRepository.save(entityToPatch);
          //...
        }      

        private Patch convertRequestToPatch(ServletServerHttpRequest request) {  
          try {
            InputStream inputStream =  new IncomingRequest(request).getBody();
            return new JsonPatchPatchConverter(objectMapper).convert(objectMapper.readTree(inputStream));
          } catch (IOException e) {
            throw new UncheckedIOException(e);
          }
        }

这篇关于具有Spring Data Rest功能的自定义Spring MVC HTTP补丁请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆