【问题标题】:Spring boot: automatic JSON message converter for @Requestbody GET doesn't workSpring boot:@Requestbody GET 的自动 JSON 消息转换器不起作用
【发布时间】:2017-07-14 23:35:19
【问题描述】:

自动消息转换不适用于下一个 Rest.GET。如何使用自动 JSON 消息转换?我在没有解决重要问题的情况下阅读了许多不同的解决方案。

据说Spring Boot有很多标准的消息转换器。

Q1:为什么消息转换失败?

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

Rest 控制器包括:

@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)

【问题讨论】:

标签: java json spring rest spring-boot


【解决方案1】:

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

将您的控制器更改为 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 的文档:

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

GET 请求不发送正文。

【讨论】:

    猜你喜欢
    • 2015-02-08
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 2018-09-07
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多