2016-09-13 17 views
0

eclipseでJerseyフレームワークを使用して、サンプルRESTful Webサービスを開発しています。単にmysqlサーバに接続してクエリを実行し、値を返します。私はそれをコンパイルして(JVM)を実行すると、私は希望の出力を得ています。RESTful Webサービスの開発中にリソースが見つかりませんでした。

私は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されています0

そして、これが私の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> 

私が使用したURLは以下のとおりであった:

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

404エラーを発生させるWSにアクセスするために使用されるURLは何ですか? – Snorky35

+0

投稿を編集しました。 – klbm9999

答えて

0

をお試しください
http://localhost:8080/<WebAppNameInTomcat>/rest/mysqlconn/Customer 

Test_dbは表示名であり、ここで使用されているものではない可能性があります(Test_db)。 Tomcatはウェブアプリケーション名をコンテキストパスとして使用します

+0

Test_dbはWebappの名前です。私はディレクトリからそれを確認した。 – klbm9999

+0

Hmmm .... Jersey 1.x or 2.x? – Snorky35

+0

遅く返事して申し訳ありませんが、2.x – klbm9999

関連する問題