I am having small sample example where i am using @RestController as we know that @RestController is combination of @Controller and @ResponseBody . In below code snippet i can get the response in json format but if I want response in XML what should I try?
@RestController
public class CreditCardController {
@Autowired
CreditCradService creditcardService;
@RequestMapping(path = "/getAllCards")
public List<CreditCard> credit() {
return creditcardService.getAllCards();
}
}
I tried couple of things which did not work:
@RequestMapping(path="/getAllCards", produces = {MediaType.APPLICATION_XML_VALUE})
To successfully return XML payload, you need the following dependency in your project:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
Next, you have to annotate your Java class you want to return from your controller with @XmlRootElement
, eg:
@XmlRootElement
public class CreditCard {
// your data class
}
and then you can finally specify the produces
attribute of your @GetMapping
like you are already doing it:
@GetMapping(value = "/getAllCards", produces = MediaType.APPLICATION_XML_VALUE)
public List<CreditCard> credit(){
return creditcartService.getAllCards();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.