SoapUI 是否将我的 REST API 的 XML 转换为 JSON?
Is SoapUI converting my REST API's XML to JSON?
我被分配了构建 REST API 的任务,该任务具有输出 XML 文件的基本身份验证。
我努力让 XML 出来而不是 JSON,但我想我明白了。我注释了@XmlRootElement 和@XmlElements,果然,当我用浏览器访问API 时,XML 返回并且浏览器甚至告诉我"This XML file does not appear to have any style information associated with it. " 耶! XML!任务完成了吧?
但是,当我使用 SoapUI 或 Postman 访问 API 时,它总是给我 JSON。告诉我没有 XML,甚至在原始文件中也没有。这是 SoapUI 和 Postman 做的事情,还是(更有可能)我是否需要更改我的代码中的某些内容以使其每次都成为 XML?
如果您能提供帮助,在此先感谢您!
这是我认为相关的代码:
Application.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);
}
}
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();
}
}
在浏览器中访问它时,我确实得到了 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)
我被分配了构建 REST API 的任务,该任务具有输出 XML 文件的基本身份验证。
我努力让 XML 出来而不是 JSON,但我想我明白了。我注释了@XmlRootElement 和@XmlElements,果然,当我用浏览器访问API 时,XML 返回并且浏览器甚至告诉我"This XML file does not appear to have any style information associated with it. " 耶! XML!任务完成了吧?
但是,当我使用 SoapUI 或 Postman 访问 API 时,它总是给我 JSON。告诉我没有 XML,甚至在原始文件中也没有。这是 SoapUI 和 Postman 做的事情,还是(更有可能)我是否需要更改我的代码中的某些内容以使其每次都成为 XML?
如果您能提供帮助,在此先感谢您!
这是我认为相关的代码:
Application.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);
}
}
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();
}
}
在浏览器中访问它时,我确实得到了 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)