gpt4 book ai didi

java - Spring 数据休息 : RepositoryRestController deserialization from URI not working

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:35:44 25 4
gpt4 key购买 nike

我的问题是从 URI 字符串反序列化实体。当我使用由 Spring Data Rest 生成的 HTTP 接口(interface)时,一切正常。我可以针对我的端点 /api/shoppingLists 发布以下 JSON,它将被反序列化为以管理员为所有者的购物 list 。

{
"name":"Test",
"owners":["http://localhost:8080/api/sLUsers/admin"]

当我使用自定义 RepositoryRestController 时,这不再起作用。如果我将完全相同的 JSON 发布到相同的端点,我会收到此响应。

{
"timestamp" : "2015-11-15T13:18:34.550+0000",
"status" : 400,
"error" : "Bad Request",
"exception" : "org.springframework.http.converter.HttpMessageNotReadableException",
"message" : "Could not read document: Can not instantiate value of type [simple type, class de.yannicklem.shoppinglist.core.user.entity.SLUser] from String value ('http://localhost:8080/api/sLUsers/admin'); no single-String constructor/factory method\n at [Source: java.io.PushbackInputStream@9cad4d2; line: 1, column: 26] (through reference chain: de.yannicklem.shoppinglist.core.list.entity.ShoppingList[\"owners\"]->java.util.HashSet[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class de.yannicklem.shoppinglist.core.user.entity.SLUser] from String value ('http://localhost:8080/api/sLUsers/admin'); no single-String constructor/factory method\n at [Source: java.io.PushbackInputStream@9cad4d2; line: 1, column: 26] (through reference chain: de.yannicklem.shoppinglist.core.list.entity.ShoppingList[\"owners\"]->java.util.HashSet[0])",
"path" : "/api/shoppingLists"
}

我的 RepositoryRestController:

@RepositoryRestController
@ExposesResourceFor(ShoppingList.class)
@RequiredArgsConstructor(onConstructor = @__(@Autowired ))
public class ShoppingListRepositoryRestController {

private final ShoppingListService shoppingListService;

private final CurrentUserService currentUserService;

@RequestMapping(method = RequestMethod.POST, value = ShoppingListEndpoints.SHOPPING_LISTS_ENDPOINT)
@ResponseBody
@ResponseStatus(HttpStatus.CREATED)
public PersistentEntityResource postShoppingList(@RequestBody ShoppingList shoppingList,
PersistentEntityResourceAssembler resourceAssembler) {

if (shoppingListService.exists(shoppingList)) {
shoppingListService.handleBeforeSave(shoppingList);
} else {
shoppingListService.handleBeforeCreate(shoppingList);
}

return resourceAssembler.toResource(shoppingListService.save(shoppingList));
}
}

谁能告诉我为什么反序列化不再适用于自定义 RepositoryRestController(由 docs 建议)?

To take advantage of Spring Data REST’s settings, message converters, exception handling, and more, use the @RepositoryRestController annotation instead of a standard Spring MVC @Controller or @RestController

完整的源代码请查看 GitHub repo

最佳答案

为了使用 HAL MessageConverter,你应该有一个 Resource 作为参数。尝试将您的代码更改为:

 public PersistentEntityResource postShoppingList(@RequestBody Resource<ShoppingList> shoppingList)

关于java - Spring 数据休息 : RepositoryRestController deserialization from URI not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33720235/

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