0

I am developing a sample RESTful web service, using Jersey framework, in eclipse. It simply connects to mysql server executes a query and returns the values. When i compile it and run (JVM), i am getting the desired ouput.

Now i am using Tomcat v8.0 server to process requests, and want the same query results in XML format. But i am getting a 404 always.

Here is my code

mysqlconn.java

package com.just_test_db;

import java.sql.*;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Path("/mysqlconn")
class mysqlconn {

@GET
@Path("/Customer")
@Produces(MediaType.APPLICATION_XML)
public void getdata() {
    try {
        Connection con = Connect_db.getConnection();
        mysql_query(con);
        mysql_connection_close(con);
    }
    catch (Exception e) {
        e.printStackTrace();
    }

}

public Customer mysql_query(Connection con) throws Exception {
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("select Customer_id, Customer_email, Customer_phone from Customers");
    Customer c = new Customer(rs.getInt(1),rs.getString(2),rs.getString(3));
    return c;
}

public void mysql_connection_close(Connection con) throws Exception {
    con.close();
}

}

here is Customer.java

package com.just_test_db;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Customer")
class Customer {
    int CustomerID;
    String Customer_email;
    String Customer_phone;

    public Customer() {}
    public Customer(int c, String e, String p) {
        this.CustomerID = c;
        this.Customer_email = e;
        this.Customer_phone = p;
    }
    public int getCustomerID() {
        return CustomerID;
    }
    @XmlElement
    public void setCustomerID(int customerID) {
        this.CustomerID = customerID;
    }

    public String getCustomer_email() {
        return Customer_email;
    }

    @XmlElement
    public void setCustomer_email(String customer_email) {
        this.Customer_email = customer_email;
    }

    public String getCustomer_phone() {
        return Customer_phone;
    }

    @XmlElement
    public void setCustomer_phone(String customer_phone) {
        this.Customer_phone = customer_phone;
    }

}

here is my Connect_db.java :

package com.just_test_db;

import java.sql.Connection;
import java.sql.DriverManager;

class Connect_db {

    static Connection con = null;
    static String dbhost = "jdbc:mysql://localhost:3306/Test";
    static String dbuser = "user";
    static String dbpass = "pass";


    public static Connection getConnection() {
        if (con != null) return con;
        return getConnection(dbhost,dbuser,dbpass);
    }

    public static Connection getConnection(String dbhost, String dbuser, String dbpass) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(dbhost,dbuser,dbpass);
            return con;
        }
        catch (Exception c) {
            System.out.println("Could not connect to the Database");
            c.printStackTrace();
        }

        return con;
    }
}

And this is my 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>Test_db</display-name>
   <servlet>
      <servlet-name>Jersey RESTful Application</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.just_test_db</param-value>
         </init-param>
      </servlet>
   <servlet-mapping>
   <servlet-name>Jersey RESTful Application</servlet-name>
      <url-pattern>/rest/*</url-pattern>
   </servlet-mapping>  
</web-app>

i expect the output something like this :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <Customer>
        <CustomerID>1</Customerid>
        <CustomerEmail>Bhanu</CustomerEmail>
        <CustomerPhone>9999999999</CustomerPhone>
    </Customer>

The URL i used was :

http://localhost:8080/Test_db/rest/mysqlconn/Customer
klbm9999
  • 31
  • 1
  • 9

1 Answers1

0

Try

http://localhost:8080/<WebAppNameInTomcat>/rest/mysqlconn/Customer

Test_db is a display name and may be not the one that is used here (Test_db). Tomcat use the WebApp name as context path

Snorky35
  • 426
  • 6
  • 15
  • Test_db is the Webapp name. i confirmed it from the directory. – klbm9999 Sep 13 '16 at 12:23
  • Sorry for the late reply, it is 2.x – klbm9999 Sep 21 '16 at 02:58
  • try this post, using the "in class" configuration ApplicationConfig class instead of web.xml : http://stackoverflow.com/questions/22022114/org-glassfish-jersey-servlet-servletcontainer-classnotfoundexception – Snorky35 Sep 21 '16 at 09:28