Problems solved in spring-rest
-
Can we have a single endpoint serving both GET & POST methods
--> Yes, we can have that.
Annottate both methods with @GetMapping() & @PostMapping with same path-url names at both the places -
Creation of a single endpoint to server both XML & JSON requests
--> Yes, it is possible
Add "jackson-dataformat-xml" dependency for conversion of JSON to XML (Version: 2.4.3)
At Server Side:
Annotate a RestController class with @Provider annotation
Specify consumer at method level as:
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
Specify producer at method level as:
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})At the client side:
Keep the request body input in JSON or XML format as required.
Add the "Accept" header with value as "application/xml" or "application/json" as per the requirementWe can send an XML and can retrieve the JSON if required
Code Sample:
--> https://github.com/sachin2smart/spring-rest/blob/master/springrestdemo/src/main/java/in/sachinshinde/springrestdemo/controller/MessageController.java