0

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);
  }
}
Theara
  • 79
  • 2
  • 12

3 Answers3

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);
        }
    }
}
ypdev19
  • 173
  • 2
  • 13
  • Hi @ypdev19 I have already compared your sample code with mind it isn't different, and now I found another root cause I just removed this dependency and try again the endpoint response as a JSON. ` com.fasterxml.jackson.dataformat jackson-dataformat-xml 2.13.2 ` – Theara Apr 05 '22 at 02:03
  • Hi @Theara ! so after removing that dependency you got the response as a JSON and without having to use the MediaType.APPLICATION_JSON_VALUE ? – ypdev19 Apr 06 '22 at 20:59
  • Yes, @ypdev19, and I try checking what's a conflict of this dependency, but not yet found the root cause. – Theara Apr 07 '22 at 01:11
  • Oh that's really good news @Theara! and i look a bit deeper into that dependency and it works for XML-based backends so it make sense that after removing it your api response format was JSON instead of XML. Here's more info: https://javadoc.io/doc/com.fasterxml.jackson.dataformat/jackson-dataformat-xml/latest/index.html – ypdev19 Apr 07 '22 at 18:50
0

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/
Dhruv sahu
  • 67
  • 1
  • 2
0

This might be the side affect of the some other dependency due to which Spring RestTemplate switches the MessageSerializer to JacksonXml and causes XML as default protocol. The similar issue was reported here as well : https://github.com/Azure/azure-sdk-for-java/issues/7694.

The work around is : create a file with name spring.properties and add to the classpath and add this property: spring.xml.ignore=true.

user2122524
  • 516
  • 1
  • 7
  • 9