2016-04-03 1 views
3

私は春のブートとジャージー(私は春のブートスタータージャージーを使用している)で、ゲートウェイのAPIに取り組んでいます。私は、XMLとJSON応答の両方を返すこととしています、リクエストがXMLのために作られたときが、私は404を取得し、ここでジャージーリターンxmlレスポンス付きの春のブート

package com.quickp.services; 

import javax.ws.rs.DefaultValue; 
import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.QueryParam; 

import org.springframework.http.MediaType; 
import org.springframework.web.bind.annotation.ResponseBody; 

import com.quickp.unit; 
import com.quickp.models.units; 
import com.quickp.serviceclient.ApiClient; 

@Path("api/units") 
public class UnitsService{ 

    private ApiClient client; 

    public UnitsService(ApiClient client){ 
     this.client = client; 
    } 

    @GET 
    @Produces({MediaType.APPLICATION_XML_VALUE,  MediaType.APPLICATION_JSON_VALUE}) 
    public @ResponseBody Units getUnits(
      @QueryParam("search") final String search, 
      @QueryParam("page") @DefaultValue("1") final int page) { 
     return client.getUnits(search, page, 10); 

    } 

} 

のpom.xmlには、次したサービスのためのコードであるJSONレスポンスで動作するようです:

<dependency> 
    <groupId>com.fasterxml.jackson.dataformat</groupId> 
    <artifactId>jackson-dataformat-xml</artifactId> 
    <version>2.5.0</version> 
</dependency> 
<dependency> 
    <groupId>com.fasterxml.jackson.jaxrs</groupId> 
    <artifactId>jackson-jaxrs-xml-provider</artifactId> 
    <version>2.5.0</version> 
</dependency> 
<dependency> 
    <groupId>org.codehaus.woodstox</groupId> 
    <artifactId>woodstox-core-asl</artifactId> 
    <version>4.4.1</version> 
</dependency> 

とUnits.classのようなものです:

@Data 
@JacksonXmlRootElement(localName = "units") 
public class Units { 
    private int found; 
    private int display; 
    private boolean hasMore; 
    @JsonProperty("unit") 
    @JacksonXmlElementWrapper(useWrapping = false) 
    List<Unit> list; 
} 

とunit.classのようなものです0

@Data 
@EqualsAndHashCode 
@JacksonXmlRootElement(localName = "unit") 
public class Unit { 
    private int id; 
    private String name; 
    private String unitType; 
    private String unitApp; 
    private String unitHomeApp; 
} 

(私はlambokを使用しているので、getterとsetterを手動で追加する必要はありません)。

すべてのおかげで、私はこれに固執しています。

よろしく sajid

答えて

2

デフォルトのXMLプロバイダは、あなたのPOJOにJAXB注釈を期待JAXBを使用しています。 Jackson XMLプロバイダを使用する場合は、登録する必要があります。これはJAXBをオーバーライドします。

public class JerseyConfig extends ResourceConfig { 
    public JerseyConfig() { 
     register(JacksonXMLProvider.class); 
     // use JacksonJaxbXMLProvider if you also want JAXB annotation support 
    } 
} 

予想される500の代わりに、404の理由はthis questionに関連しています。 This answerで問題を解決する必要がありますので、予想されるエラー応答が得られます。

関連する問題