簡體   English   中英

xml響應不起作用,但是json在使用Jersey的Rest Api運行

[英]xml response not working but json is working Rest Api using Jersey

這是我第一個使用jersey的REST API程序。 當我嘗試以XML格式返回響應時,我的rest API給我一個Error code 500 ,但它對於JSON正常工作。

有人可以告訴我我在做什么錯嗎?

控制台中沒有顯示錯誤。

    public class MyResource {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "Got it!";
    }
    @GET
    @Produces(MediaType.APPLICATION_XML)
    @Path("/cust")
    public Customer getCust() {
        Customer cus= new Customer();
        cus.setName("john");
        cus.setId(1);
        cus.setAddress("india");
        return cus;
    }
    @GET
    @Path("/test/order")
    @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
    public Order getOrder() {
        List<Order> o =new ArrayList<Order>();
        Order ord=new Order();
        ord.setCost(0);
        ord.setProductID(0);
        ord.setQuantity(0);
        ord.setShippingAddress("abcd");
        o.add(ord);

        return ord;
    }


}

我的客戶類別

    package in.octalian.mobileservice.model;

     import javax.xml.bind.annotation.XmlRootElement;

         @XmlRootElement
     public class Customer {

    private int id;
    private String name ;
    private String address;

    public Customer() {}

    public Customer(int id,String name,String address) {
        this.id=id;
        this.name=name;
        this.address=address;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

}

我的pom.xml

 <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>in.octalian</groupId>
    <artifactId>mobileservice</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>mobileservice</name>

    <build>
        <finalName>mobileservice</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
        </dependency>
        <!-- uncomment this to get JSON support -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-binding</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>

    </dependencies>
    <properties>
        <jersey.version>2.27</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

我附上的XML響應可為您提供更多信息。 在此處輸入圖片說明

問題是您嘗試訪問URI /cus但是代碼的聲明為@Path("/cust") 因此,您應該使用/cust

其次,如果在控制台中由於某些錯誤而沒有日志記錄,則可以在調試模式下運行服務器(對我來說是apache-tomcat)以獲取調試信息。

另外,您需要在請求中添加具有預期結果格式類型的標頭“ Accept”。 如果未指定,則將第一個顯示為第一個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM