java - xml 响应不起作用,但 json 正在使用 Jersey 运行 Rest Api

标签 java rest tomcat jersey

这是我在 REST API 上使用 jersey 的第一个程序。当我尝试以 XML 格式返回响应时,我的休息 API 给了我一个错误代码 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 响应可以为您提供更多信息。 enter image description here

最佳答案

问题是您正在尝试访问 URI /cus,但您的代码声明为 @Path("/cust")。所以你应该使用 /cust

其次,您可以在 Debug模式下运行您的服务器(对我来说是 apache-tomcat)以获取调试信息,如果由于某些错误而没有日志记录在控制台中的话

关于java - xml 响应不起作用,但 json 正在使用 Jersey 运行 Rest Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53466374/

相关文章:

java - 如何将 .war 文件正确部署到 OpenShift?

java - 在哪里将 robots.txt 放在 tomcat 7 中?

Java Assert语句用法,考试认证1Z0-851

java - 关于 Hadoop 中的序列化 : what are the advantages of serialization?

php - 使用 PHP 调用 AWS Api Gateway 的首选方式

AngularJS $资源拦截器

tomcat - Spring Boot 内嵌 Tomcat : how to use Tomcat 7 in Integration Tests?

java - 将两个相似的 json 字段编码到同一个 java 字段

java - 如何查询firebase数据库以返回值的键的父级的父级?

python - 使用 CherryPY MethodDispatcher 的动态 URL