I've been assigned the task of building a REST API with basic authentication that outputs an XML file.
I struggled for awhile getting XML to come out instead of JSON, but I think I got it. I've annotated @XmlRootElement s and @XmlElements and, sure enough, when I access the API with a browser, XML comes back and the browser even tells me "This XML file does not appear to have any style information associated with it. " Yay! XML! Mission accomplished, right?
However, when I access the API with SoapUI or Postman, it always gives me JSON. Tells me there's no XML, not even in the raw. Is this something SoapUI and Postman do, or (more likely) do I need to change something in my code to make it XML every time?
Thanks in advance if you are able to help!
Here is what I believe to be the relevant code:
Application.java: package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.web.context.*;
@SpringBootApplication
public class Application {
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
public SecurityWebApplicationInitializer() {
super(WebSecurityConfig.class);
}
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Address.java
package hello;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="address")
public class Address {
private String street;
private String city;
private String stateZip;
public Address() {
this.street = "123 Main Street ";
this.city = "Concord";
this.stateZip = "NC-28027";
}
public String getStreet() {
return street;
}
public String getCity() {
return city;
}
public String getStateZip() {
return stateZip;
}
@XmlElement
public void setStreet(String street) {
this.street = street;
}
@XmlElement
public void setCity(String city) {
this.city = city;
}
@XmlElement
public void setStateZip(String stateZip) {
this.stateZip = stateZip;
}
}
AddressArray.java
package hello;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="addresses")
public class AddressArray {
public Address address1 = new Address();
public Address address2 = new Address();
public Address address3 = new Address();
}
AddressController.java
package hello;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AddressController {
@RequestMapping("/address")
public @ResponseBody AddressArray addresses() {
return new AddressArray();
}
}
While accessing this in a browser I get XML back, sure enough, but here's the kind of response I get back in Postman or SoapUI, still frustratingly in JSON:
{
"address1": {
"street": "123 Main Street ",
"city": "Concord",
"stateZip": "NC-28027"
},
"address2": {
"street": "123 Main Street ",
"city": "Concord",
"stateZip": "NC-28027"
},
"address3": {
"street": "123 Main Street ",
"city": "Concord",
"stateZip": "NC-28027"
}
}