0

Automatic message conversion is not for the next Rest.GET. How can I use automatic JSON message conversion? I read many different solutions without solving the important issue.

Spring Boot is said to have a lot of standard message converters.

Q1: Why is the message conversion failing?

Q2: Should I really add the Jackson JSON converter to the message converter list? How?

The POJO object is:

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

The Rest controller consists of:

@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());
    }

The rest(Test)Template is:

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();

The error is:

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)
tm1701
  • 7,307
  • 17
  • 79
  • 168
  • I think the problem is likely to be that the request body is ignored because you are using a GET. Try changing your Controller and Rest client to use POST. http://stackoverflow.com/questions/5216567/is-this-statement-correct-http-get-method-always-has-no-message-body – samlewis Feb 24 '17 at 08:14

1 Answers1

3

The problem isn't that the converter isn't registered but the fact that it doesn't find a body to convert.

Change your controller to POST and your RestTemplate call to use that operation and it'll work:

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

From the documentation of @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 requests don't send a body.

codependent
  • 23,193
  • 31
  • 166
  • 308