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

时间:2017-05-23 13:07:39

标签: java json xml rest soapui

我已经分配了使用输出XML文件的基本身份验证构建REST API的任务。

我挣扎了一段时间让XML出来而不是JSON,但我想我明白了。我已经注释了@XmlRootElement和@XmlElements,当然,当我使用浏览器访问API时,XML会回来,浏览器甚至会告诉我"这个XML文件似乎没有任何样式信息与之相关联。 "好极了! 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"
  }
}

1 个答案:

答案 0 :(得分:-1)

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

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