I want to return XML response message after API REST call. I tried this simple test:
@RestController
public class HelloWorldRestController {
@Autowired
ApiService apiService;
@RequestMapping(value = "/api", method = RequestMethod.GET)
public ResponseEntity<VisaResponse> listAllUsers() {
VisaResponse obj = apiService.visaresponse();
return new ResponseEntity<VisaResponse>(obj, HttpStatus.OK);
}
.......
}
I tried to convert simple object to XML
public class VisaResponse {
public VisaResponse() {
jaxbObjectToXML(new VisaResponseHeader());
}
private static String jaxbObjectToXML(VisaResponseHeader customer) {
String xmlString = "";
try {
JAXBContext context = JAXBContext.newInstance(VisaResponseHeader.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // To format XML
StringWriter sw = new StringWriter();
m.marshal(customer, sw);
xmlString = sw.toString();
} catch (JAXBException e) {
e.printStackTrace();
}
return xmlString;
}
}
public class VisaResponseHeader {
private int id;
public VisaResponseHeader() {
id = 3;
}
}
But when I make a rest request nothing happens - no any response. Do you have any idea where I'm wrong? Simple content should be returned.
