簡體   English   中英

開發RESTful Web服務時獲取資源未找到的響應

[英]Getting a resource not found response while developing a RESTful web service

我正在eclipse中使用Jersey框架開發一個示例RESTful Web服務。 它只需連接到mysql服務器就可以執行查詢並返回值。 當我編譯並運行(JVM)時,我得到了所需的輸出。

現在,我正在使用Tomcat v8.0服務器來處理請求,並希望XML格式的查詢結果相同。 但我總是得到404。

這是我的代碼

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

}

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

}

這是我的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;
    }
}

這是我的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>

我期望輸出是這樣的:

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

我使用的網址是:

http://localhost:8080/Test_db/rest/mysqlconn/Customer

嘗試

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

Test_db是一個顯示名稱,可能不是此處使用的名稱(Test_db)。 Tomcat使用WebApp名稱作為上下文路徑

暫無
暫無

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

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