Я пытаюсь отправить длинный список json и взять записи из db.
Мой контроллер:
@Api(tags = Endpoint.RESOURCE_customer, description = "customer Resource")
@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE,consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public class CustomerResourceController {
private final customerService customerService;
public CustomerResourceController(customerService customerService) {
this.customerService = customerService;
}
@ApiOperation(
value = "Return customer",
response = customerDto.class, responseContainer="List"
)
@PostMapping(value = Endpoint.RRESOURCE_customer_ID)
public List<customerDto> getCustomersByIds(@RequestBody List<Long> ids) {
return customerService.findcustomerIds(ids);
}
}
А класс клиента:
@Headers("Content-Type: " + MediaType.APPLICATION_JSON_VALUE)
public interface CustomerClient {
@RequestLine("POST /customer/customers/search")
List<LocGrpDto> getCustomersByIds(@RequestBody @Validated List<Long> ids);
}
И я тестирую эту услугу в почтальоне с JSON:
{"идентификаторы": [1,7,8]}
Но я получаю такую ошибку:
{
"timestamp": "2018-10-05T13:29:57.645+0000",
"status": 400,
"error": "Bad Request",
"message": "Could not read document: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@3cb8b584; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@3cb8b584; line: 1, column: 1]",
"path": "/api/v1/customer/customers/search",
"errors": []
}
В чем проблема? Вы видите здесь какие-либо проблемы, или они могут быть вызваны моим классом обслуживания или классами dto?
1 ответ
Попробуйте запросить с полезной нагрузкой [1,7,8]
, а не {"ids": [1,7,8]}
.
Ваш JSON преобразуется в тело запроса в следующем формате.
class Body {
private List<Long> ids;
// constructor, getters and setters
}
Для клиента REST вы можете взглянуть на RestTemplate
.
RestTemplate template;
List<Long> ids;
List<CustomerDto> = template.exchange(
"/customer/customers/search",
HttpMethod.POST,
new HttpEntity<>(ids),
new ParameterizedTypeReference<List<CustomerDto>>() {}).getBody()
Похожие вопросы
Новые вопросы
java
Java - это язык программирования высокого уровня. Используйте этот тег, если у вас возникли проблемы с использованием или пониманием самого языка. Этот тег редко используется отдельно и чаще всего используется вместе с [spring], [spring-boot], [jakarta-ee], [android], [javafx], [hadoop], [gradle] и [maven].