Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在SpringDataREST中处理自定义POST PUT和修补程序请求中的关联项_Java_Spring_Spring Boot_Spring Data Rest - Fatal编程技术网

Java 在SpringDataREST中处理自定义POST PUT和修补程序请求中的关联项

Java 在SpringDataREST中处理自定义POST PUT和修补程序请求中的关联项,java,spring,spring-boot,spring-data-rest,Java,Spring,Spring Boot,Spring Data Rest,我需要在POST、PATCH和PUT端点中具有自定义业务逻辑。使用SDR事件是不可能的,因为我需要在请求中执行一些事务操作。因此,我决定为通过服务类连接到存储库的实体创建自定义端点 @RepositoryRestController @RequestMapping("/myEntity") @ExposesResourceFor(MyEntity.class) public class MyEntityResource { @PostMapping(value = "", produce

我需要在POST、PATCH和PUT端点中具有自定义业务逻辑。使用SDR事件是不可能的,因为我需要在请求中执行一些事务操作。因此,我决定为通过服务类连接到存储库的实体创建自定义端点

@RepositoryRestController
@RequestMapping("/myEntity")
@ExposesResourceFor(MyEntity.class)
public class MyEntityResource {
    @PostMapping(value = "", produces = MediaTypes.HAL_JSON_VALUE)
    public ResponseEntity postResult(@RequestBody Entity entity) {
        // my logic
    }
}
现在我面临一个问题,我的POST请求可以与其他实体建立关联链接。SDR的默认实现很好地处理了这个问题,但我面临一个Jackson映射错误

JSON解析错误:无法构造'com.foo.bar.Entity'的实例(尽管至少存在一个创建者):没有字符串参数构造函数/工厂方法从字符串值('/api/v1/Entity/12345678-1234-1234-123456789012')反序列化。

因此,我查看了Spring的实现情况,发现了以下方法

@ResponseBody
@RequestMapping(value = BASE_MAPPING, method = RequestMethod.POST)
public ResponseEntity<ResourceSupport> postCollectionResource(RootResourceInformation resourceInformation,
        PersistentEntityResource payload, PersistentEntityResourceAssembler assembler,
        @RequestHeader(value = ACCEPT_HEADER, required = false) String acceptHeader)
@ResponseBody
@RequestMapping(值=基本映射,方法=RequestMethod.POST)
公共响应收集后资源(RootResourceInformation,
PersistentEntityResource有效负载,PersistentEntityResourcesAssembler汇编程序,
@RequestHeader(值=ACCEPT_HEADER,必需=false)字符串acceptHeader)
我发现,
persistenentEntityResource有效负载
由获取的关联实体填充,主实体通过对存储库的正常保存调用保存

因此,我尝试了自动连接
persistentyresource
,但基本上失败了,因为
persistentyresource
的调用方希望映射url的形式为
/{repository}/
,并且我已经知道我的路径,
persistentyresource
无法初始化。
persistenentEntityResource
不是通用的(它是高达SDR 2.0.0.M1的,之后被删除),这也无济于事。同时能够使用
PersistentEntityResource
也会使实现补丁变得更容易


有什么办法可以解决这个问题吗?

我经过一番挖掘后找到了答案。事后看来,这是相当微不足道的。只需在方法参数中使用
Resource
而不是
Entity

@RepositoryRestController
@RequestMapping("/myEntity")
@ExposesResourceFor(MyEntity.class)
public class MyEntityResource {
    @PostMapping(value = "", produces = MediaTypes.HAL_JSON_VALUE)
    public ResponseEntity postResult(@RequestBody Resource<Entity> entity) {
        // my logic. Fetch the entity with entity.getContent()
    }
}
@RepositoryRestController
@请求映射(“/myEntity”)
@ExposeSourceFor(MyEntity.class)
公共类MyEntityResource{
@PostMapping(value=”“,products=MediaTypes.HAL\u JSON\u value)
public ResponseEntity postResult(@RequestBody资源实体){
//my logic.Fetch使用entity.getContent()获取实体
}
}

您可以使用
entity.getContent()

获取实体本身。事件处理程序是事务性的。@它们与SDR调用本身在同一事务下吗?整个SDR请求通过筛选器嵌入到事务中。