0

This is my first program on REST API using jersey. My rest API is giving me an Error code 500 when I try to get the response back in XML but it is working fine for JSON.

Can someone tell me what I am doing wrong?

There is no error shown in the console.

    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;
    }


}

My customer class

    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;
    }

}

my 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 response that I attach to give more information to you. enter image description here

Roy Scheffers
  • 3,832
  • 11
  • 31
  • 36
  • Is there a stacktrace on the server logs? Is this happening for both resource methods? You might want to try to register this [DebugExceptionMapper](https://stackoverflow.com/a/31289875/2587435). I bet you will get a stack trace with more useful information in you're not already getting one, – Paul Samsotha Nov 25 '18 at 10:16
  • yes it is happening to both the methods. i did register an exception mapper and it did not show me any stack trace. – scoutjohn13 Nov 25 '18 at 11:05
  • You sure the mapper is registered? How are you registering it? So currently the mapper is not being called? – Paul Samsotha Nov 27 '18 at 08:48

2 Answers2

1

The issue is that you are trying to access a URI /cus but your code have a declaration as @Path("/cust"). So you should be using /cust.

Secondly, you could run your server(apache-tomcat for me) in debug mode to get debugging information if no logs are getting logged in the console for some error

Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41
0

Also, you need to add the header "Accept" in the request with the expected result format type. If not specified the first one will be considered as it appears first.