【问题标题】:How to Pass an object to a REST Web Resource using Jersey如何使用 Jersey 将对象传递给 REST Web 资源
【发布时间】:2012-08-13 10:49:54
【问题描述】:

我是 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());

    }
}

Index.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;
    }

Index.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>

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

SEVERE:Java 类型、类的消息体阅读器 com.service.fruitstore.FruitBean 和 MIME 媒体类型, 应用程序/x-www-form-urlencoded,没有找到

请帮帮我

【问题讨论】:

  • 请客气地提及投反对票的原因。

标签: java jersey jax-rs


【解决方案1】:

您没有将 xml 发送到您的控制器。检查How to post XML to server thru HTML form?。

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

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

使用正确的消费和生产注解。

<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"/>

【讨论】:

  • 谢谢。我按照您指定的网址进行操作。但我仍然收到“HTTP 状态 415 ---服务器拒绝此请求,因为请求实体的格式不受请求的资源支持请求的方法()。”。请帮帮我
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    我也遇到了同样的问题。然而,我以不同的方式做到了。 我没有传入一个对象(这会更容易,但是我这样做也是出于学习目的),而是使用了@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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 2013-10-10
      相关资源
      最近更新 更多