0
自动消息转换不适用于下一个Rest.GET。我如何使用自动JSON消息转换?我读了很多不同的解决方案,但没有解决重要的问题Spring引导:用于@Requestbody的自动JSON消息转换器GET不起作用
Spring Boot据说有很多标准的消息转换器。
问题1:为什么消息转换失败? Q2:我真的应该将Jackson JSON转换器添加到消息转换器列表中吗?怎么样?
的POJO目的是:
public class CacheCoordinateRange {
private double latitudeMin;
private double latitudeMax;
private double longitudeMin;
private double longitudeMax;
public CacheCoordinateRange() { }
public CacheCoordinateRange(double latMin, double latMax, double lonMin, double lonMax) {
this.latitudeMin = latMin;
this.latitudeMax = latMax;
this.longitudeMin = lonMin;
this.longitudeMax = lonMax;
}
... getters and setters
其余控制器包括:
@RequestMapping(method = RequestMethod.GET, value = "/coordinaterange", produces = { "application/json" }, consumes = MediaType.ALL_VALUE)
public List<Items> findByCoordinateRange(@RequestBody CacheCoordinateRange coordinateRange) {
return cacheRepository.findByLatitudeBetweenAndLongitudeBetween( coordinateRange.getLatitudeMin(),
coordinateRange.getLatitudeMax(), coordinateRange.getLongitudeMin(), coordinateRange.getLongitudeMax());
}
剩余部分(试验)模板是:
CacheCoordinateRange range = new CacheCoordinateRange( 52.023456, 52.223456, -12.0234562, -12.223456);
HttpHeaders headersRange = new HttpHeaders();
headersRange.setContentType(MediaType.APPLICATION_JSON);
HttpEntity entityRange = new HttpEntity(range,headersRange);
ResponseEntity<SolvedCache[]> resultRange = restTestTemplate.exchange(solvedCacheRestServices + "/coordinaterange", HttpMethod.GET, entityRange, SolvedCache[].class);
objects = responseEntity.getBody();
的错误是:
WARN org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.util.List<nl.xyz.caches.Cache> nl.xyz.caches.SolvedCacheServices.findByCoordinateRange(nl.xyz.caches.CacheCoordinateRange)
我认为这个问题很可能是由于您使用GET而被忽略。尝试更改您的Controller和Rest客户端以使用POST。 http://stackoverflow.com/questions/5216567/is-this-statement-correct-http-get-method-always-has-no-message-body – samlewis