ski*_*kip 28 rest web-services jax-rs jersey java-ee-6
我是RESTful webservices的新手,并试图@OneToMany从独立的客户端应用程序更新我的关系,但我无法做到这一点.我正在使用Glassfish 3.1.1附带的JAX-RS的Jersey实现.
我有一个与班级A有@OneToMany关系的班级B.
MyRestClient 是我的独立客户端,它调用我在Glassfish 3.1.1上部署的RESTful Web服务.
MyRestClient.java
public class MyRestClient {
public static void main(String[] args) {
Client client = Client.create();
WebResource resource = client.resource("http://localhost:8080/myapp/rest/a/update/123");
B b1 = new B("debris");
ClientResponse response = resource.put(ClientResponse.class, b1);
System.out.println(response.getEntity(A.class).getTitle() + " has " + response.getEntity(A.class).getBList().size() + " Bs.");
}
}
Run Code Online (Sandbox Code Playgroud)
AResource 是一个EJB会话bean,我将其用作RESTful Web服务.
AResource.java
@Stateless
@Path("/a")
public class AResource {
@EJB
private AManager aManager;
@PUT
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
@Path("/update/{id}")
public Response updateA(B b, @PathParam("id") int id) {
A a = aManager.findAById(id);
a.addB(b);
return Response.status(Status.OK).entity(a).build();
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行客户端时,我收到以下错误消息: com.sun.jersey.api.client.ClientHandlerException:Java类型的消息正文编写器,类myPackage.B和MIME媒体类型,application/octet-stream,不是发现.
以下是我的独立客户端应用程序中的域对象,它调用AResourceEJB会话bean,我将其用作RESTful Web服务.
A.java
@XmlRootElement
public class A implements Serializable{
private List<B> bList = new ArrayList<B>();
public List<B> getBList() {
return bList;
}
//remaining code
}
Run Code Online (Sandbox Code Playgroud)
B.java
public class B implements Serializable {
private String text;
private A a;
@XmlTransient
public A getA() {
return a;
}
public void afterUnmarshal(Unmarshaller u, Object parent) {
this.a = (A) parent;
}
//remaining code
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮我理解为什么会这样,以及我该如何解决这个问题?
Mar*_*ula 30
在您的客户端代码中,您没有指定要发送的数据的内容类型 - 因此Jersey无法找到正确的MessageBodyWritter来序列化b1对象.
修改main方法的最后一行,如下所示:
ClientResponse response = resource.type(MediaType.APPLICATION_XML).put(ClientResponse.class, b1);
Run Code Online (Sandbox Code Playgroud)
并将@XmlRootElement注释添加到服务器和客户端的B类.
| 归档时间: |
|
| 查看次数: |
118363 次 |
| 最近记录: |