1

我是 webservice 的新成员。请帮助我。我正在尝试使用 Jersey 实现将对象传递到 webresource。但我遇到了错误

"HTTP Status 405" and description is "The specified HTTP method is not allowed for the requested resource ()."

我提到了下面的对象,网络资源方法,HTML页面

果豆:-

    @XmlRootElement(name="fruitbean")
    public class FruitBean {
        private long id;
        private String 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;
        }
   }

水果店服务:-

@Path("fruitstore")
public class FruitStore {

    @PUT
    @Path("checkIDByObject")
    @Consumes("application/xml")
    public void loadObject(FruitBean bean){
        System.out.println("====================");
        System.out.println("Fruit ID"+bean.getId()+" Name"+bean.getName());

    }
}

索引.htm:-

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test Jax-RS Object</title>
</head>
<body>
<form action="services/fruitstore/checkIDByObject" method="POST">
<table>
<tr>
    <td>ID:</td>
    <td><input type="text" name="id"></td>
</tr>
<tr>
    <td>Name:</td>
    <td><input type="text" name="name"></td>
</tr>
<tr>

    <td><input type="submit" Value="Submit"></td>
</tr>
</table>


</form>
</body>
</html>

我正在尝试运行此 index.htm。但我遇到了异常。如何使用 jersey 将对象传递给 Restfull webserice 中的 webresource 方法。请帮助我。

更新 :-

FruitStore Service:-

    @Path("fruitstore")
    public class FruitStore {
    @POST
    @Path("checkIDByObject")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)

    public void loadObject(FruitBean bean){
        System.out.println("====================");
        System.out.println("Fruit ID"+bean.getId()+" Name"+bean.getName());

    }
}

果豆:-

@XmlRootElement(name="fruitbean")
public class FruitBean {


    private long id;

    private String name;
    @XmlAttribute
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    @XmlAttribute
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

索引.html:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test Jax-RS Object</title>
</head>
<body>
<form action="services/fruitstore/checkIDByObject" method="POST" enctype="application/x-www-form-urlencoded">
<table>
<tr>
    <td>ID:</td>
    <td><input type="text" name="id"></td>
</tr>
<tr>
    <td>Name:</td>
    <td><input type="text" name="name"></td>
</tr>
<tr>

    <td><input type="submit" Value="Submit"></td>
</tr>
</table>


</form>
</body>
</html>

我在控制台中收到以下消息

严重:找不到 Java 类型、com.service.fruitstore.FruitBean 类和 MIME 媒体类型 application/x-www-form-urlencoded 的消息正文阅读器

请帮我

4

3 回答 3

2

您没有将 xml 发送到您的控制器。检查如何通过 HTML 表单将 XML 发布到服务器?.

Fruitbean:为 getter 或字段添加注释

编辑:您可以使用rest-client测试您的网络服务

编辑2:

@Path("fruitstore")
public class FruitStore {

  @POST
  @Path("/checkobjectbyid")
  @Consumes(MediaType.APPLICATION_XML)
  public void loadObject(FruitBean bean) {
    System.out.println("====================");
    System.out.println("Fruit ID" + bean.getId() + " Name" + bean.getName());
  }

  @GET
  @Path("/fruitbean")
  @Produces(MediaType.APPLICATION_XML)
  public Response getFruitBean(){
    FruitBean fruitBean = new FruitBean();
    fruitBean.setId(1L);
    fruitBean.setName("name of fruitbean");
    return Response.status(Response.Status.OK).entity(fruitBean).build();
  }
}

路径使用小写字符。(网址是小写的)

使用正确的 Consume 和 Produces 注释。

<servlet>
  <servlet-name>ServletAdaptor</servlet-name>
  <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>ServletAdaptor</servlet-name>
  <url-pattern>/resources/*</url-pattern>
</servlet-mapping>

web.xml 的一部分

网址:

POST http://localhost:8080/PROJECTNAME/resources/fruitstore/checkobjectbyid

GET http://localhost:8080/PROJECTNAME/resources/fruitstore/fruitbean

使用 rest-client 进行测试

URL: http://localhost:8080/PROJECTNAME/resources/fruitstore/checkobjectbyid

METHOD: POST

CONTENT-TYPE: application/xml

CHARSET: UTF-8

BODY: <?xml version="1.0" encoding="UTF-8" standalone="yes"?><fruitbean id="1" name="name of fruitbean"/>
于 2012-08-13T11:00:32.793 回答
0

首先:在您的index.htm:method="POST"和网络服务代码中:@PUT。那么这不是表单操作 - 这只是您的请求的正文。尝试按照 Err的建议使用 rest-cliet或Chrome cREST Client

于 2012-08-13T11:22:10.633 回答
0

我也有同样的问题。然而,我以不同的方式做到了。我没有传入一个对象(这会更容易,但是我也是出于学习目的这样做),而是使用了@FormParam注释。

HTML

<form action="../{projectname}/{rest}/{resource}/findByIdOrName" action="post">
    <label>id</label>
    <input type="text" name="id" />
    <label>name</label>
    <input type="text" name="name" />
    <input type="submit" value="Find" />
</form>

JAX-RS 资源

@Path("/resource")
public class resource {

    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Path("findByIdOrName")
    public void findByIdOrName (@FormParam("id") int id, 
                                @FormParam("name") String name) {
        System.out.println("=======");
        System.out.println("id: " + id + " name:" + name);
    }
}

我就是这样做的,它很简单而且很有效。至于模型,老实说,我不知道。我更像是一个 C#.NET 人,我正在尝试学习 Java。

于 2015-04-08T16:00:18.930 回答