1

I am new to the restful webservices. I am new creating restful webservice in java using jersey . This webservice is getting data from mysql database and should display the response in xml.. But i am always getting the response error 500 from apache tomcat 7. In the console no error or exception is shown except the println method is diplaying the strings passed to it..but server is giving 500 error.. Please help me

userData.java

 package com.userdb;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class userData {
    public String name;
    public int iduser;

    public userData(){}


    public userData(String name, int iduser) {
        this.name = name;
        this.iduser = iduser;
    }


    public String getName() {
        return name;
    }
    public int getIduser() {
        return iduser;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setIduser(int iduser) {
        this.iduser = iduser;
    }


} 

airtime.java

package com.userdb;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


@Path("/resttest")
public class airtime {
    ResultSet rs=null;
    String msg="hello";
    Connection con=null;


    @GET
    @Produces(MediaType.APPLICATION_XML)
    @Path("/get_users")
    public List<userData> get_users(){
        List<userData> retUser=new ArrayList<>();
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "");
            System.out.println("DriveManager");
        PreparedStatement     ps=con.prepareStatement("SELECT * FROM users");
            rs=ps.executeQuery();
            System.out.println(rs);
            while(rs.next()){
                userData obj=new userData();
                obj.setIduser(rs.getInt("iduser"));
                obj.setName(rs.getString("name"));
            retUser.add(obj);
                System.out.println("userData obj added to list");
                }
            con.close();

        } catch (Exception e){
            e.printStackTrace();
        } 
        return retUser;
    }
} 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>userdb</display-name>

  <servlet>
  <servlet-name>Jersey WebService</servlet-name>
  <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  <init-param>
  <param-name>jersey.config.server.provider.packages</param-name>
  <param-value>com.userdb</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>Jersey WebService</servlet-name>
  <url-pattern>/api/*</url-pattern>
  </servlet-mapping>

</web-app>
1
  • So what URL are you requesting in your HTTP client? What client are you using? With what parameters? Do you use GET method? If you add the HTTP request / response, etc to your question we can answer it quicker Commented Mar 20, 2016 at 2:52

2 Answers 2

1

Solution 1: Adjust the top of userData.java to contain the following lines at the top your file:

package com.userdb;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class userData 
{...}

Explanation: You will notice that an XmlAccessorType annotation and an XMLAccessType has been added. This configuration is required when using setters in your object. You will notice that adjusting the code to not use setters (with the above excluded) will also allow you to view your RESTful service in a browser.

Solution 2: A quicker alternative, is to set your public variables name and iduser to private. This will also avoid the clash when mapping to xml.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Gregory Nikitas ....... I just changed the variable's access specifier to private... And got the xml response from server .... I was stuck to this from 3 days.... Again thanks :)
0

There should be a warning something like "...messagebodywriter not found for media type=application/xml ...".

The possible cause may be that you didn't include this dependency.

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-jaxb</artifactId>
        <version>2.21.1</version>
    </dependency>

this is my workable pom dependencies

<dependencies>

        <!-- jersey dependency -->
        <dependency>
            <groupId>org.glassfish.jersey.bundles</groupId>
            <artifactId>jaxrs-ri</artifactId>
            <version>2.21.1</version>
        </dependency>

        <!-- make jersey auto marshel to json -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>2.21.1</version>
        </dependency>


        <!-- make jersey auto marshel to xml -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-jaxb</artifactId>
            <version>2.21.1</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.woodstox</groupId>
            <artifactId>stax2-api</artifactId>
            <version>3.1.4</version>
        </dependency>

    </dependencies>

a workable web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>Rest</display-name>

    <servlet>
        <servlet-name>MyApplication</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.blithe.resource</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.scanning.recursive</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyApplication</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

</web-app>

workable resource

package com.blithe.resource;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.blithe.model.User;

@Path("helloworld")
public class HelloWorldResource
{     
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public List<User> getStringArray(){

        List<User> list = new ArrayList<>();
        User u = new User();
        u.setName("testName");
        list.add(u);
        return list;

    }   
}

and the model

package com.blithe.model;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class User {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


}

Hope this will help you.

By the way, it is better not to include too many dependencies which might duplicate in your pom.xml, since some of them might conflict to each other. And it will cost a lot of time on debugging. So it's better to find out what dependencies are really needed.

Cheers!

Gregory Nikitas is right. The issue in your model class should also be taken care of.

1 Comment

I am new to restful webservices and i m not using any dependencies in my project....i have just added manually all the jersey .jar files to web-inf>lib folder.there i also have jaxb -api-2.2.7.jar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.