Mir wurde die Aufgabe zugewiesen, eine REST-API mit Basisauthentifizierung zu erstellen, die eine XML-Datei ausgibt.Wandelt SoapUI das XML meiner REST API in JSON um?
Ich kämpfte für eine Weile XML anstelle von JSON zu kommen, aber ich denke, ich habe es. Ich habe @XmlRootElement s und @XmlElements kommentiert und, wenn ich mit einem Browser auf die API zugreife, kommt XML zurück und der Browser sagt mir sogar, dass dieser XML-Datei keine Stilinformationen zugeordnet sind. Yay! XML! Mission erfüllt, oder?
Allerdings, wenn ich die API mit SoapUI oder Postman zugreifen, gibt es mir immer JSON. Sagt mir, dass es kein XML gibt, nicht einmal im Rohzustand. Ist das etwas, was SoapUI und Postman tun, oder (wahrscheinlicher) muss ich etwas in meinem Code ändern, um es jedes Mal XML zu machen?
Vielen Dank im Voraus, wenn Sie helfen können! Hier
ist, was ich glaube, dass der entsprechende Code sein:
Application.java: Paket hallo;
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();
}
}
Während dies in einem Browser erhalte ich XML zurück, sicher genug , aber hier ist das Ki nd der Antwort bekomme ich zurück in Postman oder SoapUI, noch frustrierende 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"
}
}
Ich denke, Sie sollten hinzufügen 'produziert = {MediaType.APPLICATION_XML_VALUE}' in '@ RequestMapping' – fiskra
Eine andere Sache, die Sie tun können, ist, fügen Sie" Accept "-Taste -" Application/Xml "-Wert in den Header, während Sie Ihre Erholung Service verbrauchen – fiskra
Das macht den Trick! Problem gelöst –