Разрешение URI объекта в пользовательском контроллере (Spring HATEOAS)
У меня есть проект, основанный на Spring-Data-Rest, а также он имеет несколько пользовательских конечных точек.
Для отправки данных POST я использую json как
{
"action": "REMOVE",
"customer": "http://localhost:8080/api/rest/customers/7"
}
Это хорошо для spring-data-rest, но не работает с пользовательским контроллером.
например:
public class Action {
public ActionType action;
public Customer customer;
}
@RestController
public class ActionController(){
@Autowired
private ActionService actionService;
@RestController
public class ActionController {
@Autowired
private ActionService actionService;
@RequestMapping(value = "/customer/action", method = RequestMethod.POST)
public ResponseEntity<ActionResult> doAction(@RequestBody Action action){
ActionType actionType = action.action;
Customer customer = action.customer;//<------There is a problem
ActionResult result = actionService.doCustomerAction(actionType, customer);
return ResponseEntity.ok(result);
}
}
Когда я звоню
curl -v -X POST -H "Content-Type: application/json" -d '{"action": "REMOVE","customer": "http://localhost:8080/api/rest/customers/7"}' http://localhost:8080/customer/action
У меня есть ответ
{
"timestamp" : "2016-05-12T11:55:41.237+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 model.user.Customer] from String value ('http://localhost:8080/api/rest/customers/7'); no single-String constructor/factory method\n at [Source: java.io.PushbackInputStream@73af10c6; line: 1, column: 33] (through reference chain: api.controller.Action[\"customer\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class logic.model.user.Customer] from String value ('http://localhost:8080/api/rest/customers/7'); no single-String constructor/factory method\n at [Source: java.io.PushbackInputStream@73af10c6; line: 1, column: 33] (through reference chain: api.controller.Action[\"customer\"])",
"path" : "/customer/action"
* Closing connection 0
}
потому что Spring не может преобразовать URI в сущность Customer.
Есть ли способ использовать механизм Spring-Data-Rest для разрешения сущностей по их URI?
У меня есть только одна идея - использовать пользовательский JsonDeserializer с разбором URI для извлечения entityId и отправки запроса в хранилище. Но эта стратегия не поможет мне, если у меня есть URI, как "HTTP: // локальный: 8080 / API / отдых / клиенты / 8 / продукт«В таком случае у меня нетКод товара значение.