【问题标题】:Spring Data Rest: RepositoryRestController deserialization from URI not workingSpring Data Rest:来自 URI 的 RepositoryRestController 反序列化不起作用
【发布时间】: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

【问题讨论】:

标签: java spring jackson spring-data spring-data-rest


【解决方案1】:

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

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

【讨论】:

  • 当时我问了这个问题,ShoppingList 扩展了 ResourceSupport
【解决方案2】:

在我的情况下,问题是 pojo 的字段名称和 json 字段之间的差异。即:

@Entity
public class Pojo{

   @ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
   @JoinColumn(name = "fk_myother")
   public Other myOther;
...etc
}

POST -> /dao/pojos
{
   "myother":"http://localhost:8034/dao/others/50"
}

请注意,json 字段应改为“myOther”。

希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-16
    • 2020-06-01
    • 1970-01-01
    • 2018-10-06
    • 2017-07-04
    • 2015-09-28
    • 2018-03-18
    • 2023-03-14
    相关资源
    最近更新 更多