java 如何使用 Jersey 将对象传递给 REST Web 资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11932824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Pass an object to a REST Web Resource using Jersey
提问by user1357722
Iam a new bie in webservice.Please help me.I am trying to pass an object into webresource using Jersey Implementation.But i got error
我是 webservice 的新手。请帮助我。我正在尝试使用 Jersey 实现将对象传递到 webresource。但是我遇到了错误
"HTTP Status 405" and description is "The specified HTTP method is not allowed for the requested resource ()."
I mentioned the below object ,web resouce method,Html page
我提到了下面的对象,网络资源方法,Html 页面
FruitBean:-
水果豆:-
@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;
}
}
FruitStore Service:-
水果店服务:-
@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:-
索引.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>
Iam trying to run this index.htm.But i got exception.How to pass an object into webresource method in Restfull webserice using jersey.Please Help me.
我正在尝试运行此 index.htm。但是我遇到了异常。如何使用 jersey 将对象传递到 Restfull webserice 中的 webresource 方法中。请帮助我。
Update :-
更新 :-
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());
}
}
FruitBean:-
水果豆:-
@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:
<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>
I got below message in the console
我在控制台中收到以下消息
SEVERE: A message body reader for Java type, class com.service.fruitstore.FruitBean, and MIME media type, application/x-www-form-urlencoded, was not found
严重:未找到 Java 类型、com.service.fruitstore.FruitBean 类和 MIME 媒体类型 application/x-www-form-urlencoded 的消息正文读取器
Please help me
请帮我
回答by Err
you are not sending xml to your controller. Check How to post XML to server thru HTML form?.
您没有将 xml 发送到您的控制器。检查如何通过 HTML 表单将 XML 发布到服务器?.
Fruitbean: add annotations to the getters or fields
Fruitbean:向 getter 或字段添加注释
Edit: you can test your webservice with rest-client
编辑:您可以使用rest-client测试您的网络服务
Edit2:
编辑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();
}
}
Use lowercase characters for path. (url's are lowercase)
路径使用小写字符。(网址是小写的)
Use correct Consume and Produces annotations.
使用正确的 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>
Part of web.xml
web.xml 的一部分
urls:
网址:
POST http://localhost:8080/PROJECTNAME/resources/fruitstore/checkobjectbyid
GET http://localhost:8080/PROJECTNAME/resources/fruitstore/fruitbean
Testing with rest-client
使用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"/>
回答by Alex Stybaev
First of all: in your index.htm
: method="POST"
and in the web-service code: @PUT
. Then this is no a form action - this is just a body of your request. Try using rest-cliet as Err suggestedor a Chrome cREST Client.
首先:在您的index.htm
:method="POST"
和网络服务代码中:@PUT
。那么这不是表单操作 - 这只是您请求的主体。尝试按照 Err建议使用 rest-cliet或Chrome cREST Client。
回答by Jason Cidras
I was having the same problems too. However, I did it a different way.
Instead of passing in an object (which would be easier, however, I am also doing this for learning purposes), I used the @FormParam
annotation.
我也遇到了同样的问题。但是,我以不同的方式做到了。我没有传入一个对象(这会更容易,但是,我这样做也是为了学习目的),我使用了@FormParam
注释。
HTML
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 Resource
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);
}
}
This is how I did it, it's easy and it works. As for models, I honestly do not know.. I'm more of a C#.NET guy and I'm trying to learn Java.
我就是这样做的,它很简单,而且很有效。至于模型,老实说我不知道..我更像是一个 C#.NET 家伙,我正在努力学习 Java。