【发布时间】:2016-02-16 15:46:46
【问题描述】:
我的问题是从 URI-String 中反序列化实体。
当我使用 Spring Data Rest 生成的 HTTP 接口时,一切正常。
我可以针对我的端点/api/shoppingLists 发布以下 JSON,它将被反序列化为一个以管理员为所有者的购物清单。
{
"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 建议)?
要利用 Spring Data REST 的设置、消息转换器、异常处理等,请使用 @RepositoryRestController 注释而不是标准 Spring MVC @Controller 或 @RestController
完整的源代码请查看GitHub repo
【问题讨论】:
-
你找到解决上述问题的方法了吗,我也面临同样的问题
-
很遗憾没有抱歉:(
-
MH 所以还在等待修复 ;)
标签: java spring jackson spring-data spring-data-rest