gpt4 book ai didi

java - Spring 启动: automatic JSON message converter for @Requestbody GET doesn't work

转载 作者:行者123 更新时间:2023-12-01 20:55:36 25 4
gpt4 key购买 nike

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

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

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

问题 2:我真的应该将 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 Controller 包括:

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

最佳答案

问题不在于转换器未注册,而在于它找不到要转换的正文。

将 Controller 更改为 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的文档:

Annotation indicating a method parameter should be bound to the body of the web request. The body of the request is passed through an HttpMessageConverter to resolve the method argument depending on the content type of the request. Optionally, automatic validation can be applied by annotating the argument with @Valid. Supported for annotated handler methods in Servlet environments.

GET 请求不发送正文。

关于java - Spring 启动: automatic JSON message converter for @Requestbody GET doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42433512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com