PostForEntity: 415 Unsupported Media Type

78 Views Asked by At

In our project, we call an external API with postForEntity and receive the following: error:org.springframework.web.server.ResponseStatusException: 415 UNSUPPORTED_MEDIA_TYPE "Could not determine reason or message. Plain body: ""

final ActionDto actionDto = new ActionDto ().setRequestId(requestId);
final ResponseEntity<ActionResponseDto> response = restTemplate.postForEntity(url, actionDto, ActionResponseDto.class);

I managed to fix this error by using exchange instead of postForEntity and adding a media type:

final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
val entity = new HttpEntity(actionDto , headers);
final ResponseEntity<ActionResponseDto> response =
        restTemplate.exchange(
                url,
                HttpMethod.POST,
                entity,
                ActionResponseDto.class);

My question is the following, could you give me some hints why before with postForEntity it worked and now suddenly it doesn't? I also checked the external API and the method looks like bellow, and nothing has changed:

@PostMapping("/action/start")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "Start the action")
public ResponseEntity<ActionResponseDto> startAction(@RequestBody final ActionDto requestDto) {
    .....
}

Could it be due to some dependencies? This error is very strange... Thank you! :)

0

There are 0 best solutions below