0

I want to make a Post request with resttemplate, but the body shall be null.

 ResponseEntity<myObject> result = restTemplate.postForEntity(getBaseUrl(), null , myObject.class);

When I do this I get

org.springframework.web.client.HttpClientErrorException$UnsupportedMediaType: 415 : [{"timestamp":"2021-06-30T12:36:29.760+00:00","status":415,"error":"Unsupported Media Type","path":"/api/v1"}] at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:133) at

My Controller looks like this:

@RestController
@RequestMapping(path = "/api/v1")
public class ApiController {

    @ResponseStatus(code = HttpStatus.CREATED)
    @PostMapping(consumes="application/json",produces="application/json")
    public ExecutionMetaData execute(@RequestBody(required = false) @Nullable ParameterDTO ParameterDTO)  
    {
       doSomeThing();
    }
}

Why doesn't it work?

  • 2
    @user7294900 this is not exactly the same question! You have to pass a header indicating the content type if you pass null in the body. – Simon Martinelli Jun 30 '21 at 13:01
  • @SimonMartinelli You can add header without body as in duplicate answers – Ori Marko Jun 30 '21 at 13:20
  • 1
    @user7294900 sure that's what I say. But the duplicate post doesn't directly answer this question! You shouldn't delete a question that is no 100% a duplicate! – Simon Martinelli Jun 30 '21 at 13:34
  • Create one http header and set content type as application/json. Then create a httpEntity set the header and set null body. new HttpEntity<>(null, header), add this restTemplate call instead null. – S. Anushan Jul 04 '21 at 06:49

0 Answers0