2017-05-23 6 views
0

XMLファイルを出力する基本認証でREST APIを構築するタスクが割り当てられました。SoapUIがREST APIのXMLをJSONに変換していますか?

私はJSONの代わりにXMLを出すのに苦労しましたが、私はそれを得たと思います。 @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を取得し、ブラウザでこれをアクセス中しかし、ここではキ応答のND私はJSONでまだイライラ、バックポストマンやSOAPUIで取得:

{ 
    "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" 
    } 
} 
+0

私はあなたが追加すべきだと思います'@ RequestMapping'に' = {MediaType.APPLICATION_XML_VALUE} 'を生成する – fiskra

+0

もう一つは、" Accept "キー - " application/xml "値をヘッダーに追加して、残りのサービスを利用します。 – fiskra

+0

それはトリックです!問題が解決しました –

答えて

-1

あなたは、出力XMLにしたい場合は、次のようにあなたのRequestMapping注釈を変更する必要があります。

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

はい、またはSpringが提供する定数については、[here](https://stackoverflow.com/questions/26676613/multiple-scenarios-requestmapping-produces-json-xml-together-with-accept-or-res)の例を参照してください。 )(SOポスト)。 –

+0

それはトリックでした!どうもありがとうございます! –

+0

これを最後の文として追加するには、ブラウザでJSONをレスポンスに、Postman/SoapUIをxmlに相当するように見えます。また、コントローラの応答タイプを固定に設定しなかったため、さまざまな種類のデータ型応答(xml/json)が発生している可能性がありました。 –

関連する問題