SoapUI 是否将我的 REST API 的 XML 转换为 JSON?



我被分配的任务是构建一个具有基本身份验证的 REST API,该 API 输出一个 XML 文件。

我努力了一段时间让XML而不是JSON出来,但我想我明白了。我已经注释了@XmlRootElement s和 @XmlElements,果然,当我使用浏览器访问 API 时,XML 会返回,浏览器甚至告诉我"此 XML 文件似乎没有任何与之关联的样式信息。耶!.XML!任务完成了,对吧?

但是,当我使用 SoapUI 或 Postman 访问 API 时,它总是给我 JSON。告诉我没有 XML,甚至在原始文件中也没有。这是 SoapUI 和 Postman 做的事情,还是(更有可能)我需要每次都更改代码中的某些内容以使其成为 XML?

如果您能够提供帮助,请提前感谢您!

以下是我认为是相关代码的内容:

应用.java: 包装你好;

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);
}
}

地址.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;
}

}

地址数组.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();
}

地址控制器.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();
}

}

在浏览器中访问它时,我得到了XML,果然,但是这是我在Postman或SoapUI中得到的那种响应,仍然令人沮丧地使用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"
}
}

如果要输出XML,则应将RequestMapping注释更改为以下内容:

@RequestMapping(value = "/address", produces = MediaType.APPLICATION_XML_VALUE)

最新更新