gpt4 book ai didi

spring-boot - 在 spring data rest 中覆盖 PATCH

转载 作者:行者123 更新时间:2023-12-05 07:36:44 25 4
gpt4 key购买 nike

我想用我自己的 Controller 覆盖 spring-data-rest 中资源的 PATCH 方法。我目前有这个:

@RepositoryRestResource
interface ItemRepository extends JpaRepository<Item, Long> {
}

@RepositoryRestController
@Slf4j
class ItemController {

@Autowired
private ItemRepository itemRepository;

@PatchMapping("/items/{id}")
public void updateItem(@PathVariable("id") Long id, @RequestBody ItemDTO itemDTO) {
log.info("Updating item {}", id);
Item found = itemRepository.findOne(id);
found.name = itemDTO.name;
itemRepository.save(found);
}
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class ItemDTO {
String name;
}

我可以通过调用来创建一个项目:
curl -X POST\
http://localhost:8080/项目\
-H '内容类型:应用程序/json'\
-d'{
“名称”:“一个项目”
}'

但是如果我随后尝试使用 PATCH 更新项目:
curl -X补丁\
http://localhost:8080/items/1\
-H '内容类型:应用程序/json'\
-d'{
“名称”:“更新”
}'
该请求由我自己的 Controller 处理,但应用程序也会抛出此堆栈跟踪并以状态代码 400 进行响应。

2018-02-27 16:58:55.510 ERROR 16112 --- [o-auto-1-exec-1] o.s.d.r.w.RepositoryRestExceptionHandler : I/O error while reading input message; nested exception is java.io.IOException: Stream closed

org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message; nested exception is java.io.IOException: Stream closed
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:229) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:150) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:128) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.27.jar:8.5.27]

我在这里创建了一个示例项目(带有演示错误的测试):https://github.com/JanRenkin/springdatarest

最佳答案

使用@BasePathAwareController 注释,您可以覆盖 spring 数据 rest 路径的默认实现。为避免发生错误,您的方法应返回该实体。

@BasePathAwareController
@Slf4j
class ItemController {

@Autowired
private ItemRepository itemRepository;

@PatchMapping("/items/{id}")
public ResponseEntity<Item> updateItem(@PathVariable("id") Long id, @RequestBody
ItemDTO itemDTO) {
log.info("Updating item {}", id);
Item found = itemRepository.findOne(id);
found.name = itemDTO.name;
Item savedItem =itemRepository.save(found);
return new ResponseEntity<>(savedItem , HttpStatus.OK);
}

关于spring-boot - 在 spring data rest 中覆盖 PATCH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49024556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com