在java中调用rest服务方法时出现404错误(RestEasy)

时间:2017-07-10 21:29:51

标签: java rest resteasy

我设计了一个简单的服务,将客户详细信息插入到db并从中检索数据。但是,当我呼叫任何服务时,我收到404错误。 我在下面提供了我的代码。 Customer.java

package com.fetch;

import java.io.Serializable; 
import javax.persistence.Column;
import javax.persistence.Id;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "Customer")
@XmlType(name = "Customer")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer //implements Serializable 
{
@Id
private long id;

@Column(name = "NAME")
private String name;

public Customer(int id, String name)
{
    this.id = id;
    this.name = name;
}
public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
}

MyAccount.Java

package com.fetch;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.persistence.Entity;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; 
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

//@WebService()
//@Entity
@Path("user")
public class MyAccount
{
@POST
@Path("fetch")
//@WebMethod(operationName = "insert")
@Consumes(MediaType.TEXT_PLAIN)
public String insert(@QueryParam("name") String name) 
{
    try 
    {
       // Class.forName("com.mysql.jdbc.Driver");
       // Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentinfo", "root", "root");
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");


        String query = "insert into CUSTOMER"+"(NAME) VALUES"+"(?)";

        PreparedStatement st = con.prepareStatement(query);
        st.setString(1,name);
        st.executeUpdate();     
    } 
    catch (Exception e) 
    {
        System.out.println(e.getMessage());
    }
    return"Record inserted successfully";
}

@GET
@Path("retrive")
@Produces("text/html")
//@WebMethod(operationName = "retrive")
public String retrive() 
{
    ResultSet rs = null;
    String details = ""; 
    try 
    {
        //Class.forName("com.mysql.jdbc.Driver");
        //Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentinfo", "root", "root");
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");

        String query = "select ID,NAME from CUSTOMER";

        PreparedStatement st = con.prepareStatement(query);
        rs = st.executeQuery();

        details = "<html><body>"; 
        details = details + "<table border=1>";
        details = details + "<tr><td><Strong>Id </Strong></td>" +
                                "<td><Strong>Name </Strong></td>" + "</tr>";
        while (rs.next()) 
        {
            details = details + "<tr><td>" + rs.getInt("ID") + "</td>" +
                                    "<td>" + rs.getString("NAME") + "</td></tr>";
        }
        details += "</table></body></html>"; 
    } 
    catch (Exception e) 
    {
        System.out.println(e.getMessage());
    }   
    return details;
}
}

insert.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" 
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">
</script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#call').click(function ()
        {
            $.ajax({
                type: "post",
                url: "rest/user/fetch", //this is my servlet
                data: "name=" +$('#name').val(),
                success: function(msg){      
                        $('#output').html(msg);
                }
            });
        });
        $('#take').click(function ()
                {
                    $.ajax({
                        type: "get",
                        url: "rest/user/retrive", 
                        success: function(msg){      
                                $('#output').html(msg);
                        }
                    });
                });
    });
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="">
<!-- <form action="rest/user/fetch" method="POST"> -->
    <p>
        Name : <input id="name" name="name" />
    </p>
    <input type="button" name="call" id="call" value="Insert"/>
<!-- </form> -->
<!-- <form action="rest/user/retrive" method="GET"> -->
    <input type="button" name="take" id="take" value="Retrive"/>
<!--  </form> -->
<p>
Response : <div type="text" id="output"></div>
</p>
</form>
</body>
</html>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>FetchAndInsert</display-name>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
    <servlet-name>REST Servlet</servlet-name>
    <servlet-
class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.fetch</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>REST Servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

JAR DETAILS:

javax.ws.rs-api-2.0-m02.jar
ojdbc14.jar
persistence-api-2.0.jar

1 个答案:

答案 0 :(得分:0)

  1. 检查其余服务是否成功启动且日志没有失败。
  2. 您是否在jar列表中包含了jersey API?
  3. 您是否使用正确的网址呼叫网络服务? (例如:http:servername:portno / FetchAndInsert / rest / user / fetch with POST and http:servername:portno / FetchAndInsert / rest / user / retrive as GET)
  4. 检查是否需要POST或GET来获取Web服务?
  5. com.sun.jersey是旧的API。你可以使用glassfish版本。