'Rest API response as XML format, calling from Spring Boot with Resttemplate

I have created Rest API using Spring Boot & Data JPA. It's working fine if requested from Postman responds as JSON format, but when I request from coding using Resttemplate it responds as XML format, then I try to add
@PostMapping(value = "/xxx", produces = MediaType.APPLICATION_JSON_VALUE)
Then, I try to request using Resttemplate again it responds in JSON format.
My question what is the matter if I don't use produces = MediaType.APPLICATION_JSON_VALUE), before I don't use it, and my services work fine. I using Spring version 2.5.7 Controller

@PostMapping(value = "/xxx")
public ResponseEntity<ResponseXXX> calculateFxRate(@RequestBody XXX xxx,
                                                       @RequestHeader Map<String, String> headers) {
  ResponseXXX xxx = new ResponseXXX();
  try {
     return new ResponseEntity<>(xxx, HttpStatus.OK);
  } catch (Exception e) {
     return new ResponseEntity<>(xxx, HttpStatus.OK);
  }
}


Solution 1:[1]

With Spring Boot web, Jackson dependency is implicit, so you shouldn't need to do any explicit json conversion in your controller.

Your code looks ok, you could check at the top of your controller class if you have the @RestController annontation, with all that should be enough to. Other possible thing you could check is how your response object is implemented.

Also check out this: Returning JSON object as response in Spring Boot

I kinda did my version of your code so you can compare

@RestController
@RequestMapping(path = "baseurl")
public class TestController {

    @PostMapping(value = "/xxx")
    public ResponseEntity<YourResponseObject> calculateFxRate(@RequestBody YourRequestObject xxx,
                                                              @RequestHeader Map<String, String> headers) {
        YourResponseObject response = new YourResponseObject();
        try {
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.ok(response);
        }
    }
}

Solution 2:[2]

consuming-xml-in-spring-boot-rest

See, you are trying to interact with the XML payload as Input in your rest API. Then you should refer to the below link to design the payload as per your application.

https://www.appsdeveloperblog.com/consuming-xml-in-spring-boot-rest/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ypdev19
Solution 2