2017-02-24 58 views
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) 
+0

我认为这个问题很可能是由于您使用GET而被忽略。尝试更改您的Controller和Rest客户端以使用POST。 http://stackoverflow.com/questions/5216567/is-this-statement-correct-http-get-method-always-has-no-message-body – samlewis

回答

3

问题不在于转换器未注册,而在于它没有找到要转换的实体。

更改您的控制器POST和你RestTemplate呼叫使用操作,它会工作:

@RequestMapping(method = RequestMethod.POST, 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()); 
} 

ResponseEntity<SolvedCache[]> resultRange = restTestTemplate.exchange(solvedCacheRestServices + "/coordinaterange", HttpMethod.POST, entityRange, SolvedCache[].class); 
objects = responseEntity.getBody() 

从@RequestBody的文档:

注解指示方法参数应该绑定到Web请求的主体 。请求的正文通过 HttpMessageConverter来解析方法参数,具体取决于请求的内容类型 。或者,自动验证可以通过使用@Valid注释参数来应用 。在Servlet环境中支持 带注释的处理程序方法。

GET请求不发送正文。