找不到用于Java类型myPackage.B和MIME媒体类型application/octet-stream的消息正文编写器 [英] A message body writer for Java type, class myPackage.B, and MIME media type, application/octet-stream, was not found

查看:433
本文介绍了找不到用于Java类型myPackage.B和MIME媒体类型application/octet-stream的消息正文编写器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是RESTful Web服务的新手,正在尝试通过独立的客户端应用程序更新我的@OneToMany关系,但是我无法做到这一点.我正在使用Glassfish 3.1.1附带的JAX-RS的Jersey实现.

I am new at RESTful webservices and was trying to update my @OneToMany relationship from a standalone client application, but I am not able to do that. I am using the Jersey implementation of JAX-RS that ships with Glassfish 3.1.1.

我有一个与类B@OneToMany关系的类A.

I have a class A that has a @OneToMany relationship with class B.

MyRestClient是我的独立客户端,正在调用已在Glassfish 3.1.1上部署的RESTful Web服务.

MyRestClient is my standalone client that is calling my RESTful webservice which has been deployed on Glassfish 3.1.1.

MyRestClient.java

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.");
    }
}

AResource是我用作RESTful Web服务的EJB会话bean.

AResource is an EJB session bean which I am using as RESTful webservice.

AResource.java

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

运行客户端时,出现以下错误消息: com.sun.jersey.api.client.ClientHandlerException:找不到用于Java类型myPackage.B和MIME媒体类型application/octet-stream的消息正文编写器.

When I run the client I get the following error message: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class myPackage.B, and MIME media type, application/octet-stream, was not found.

以下是我的独立客户端应用程序中的域对象,该应用程序正在调用AResource EJB会话Bean,我将其用作RESTful Web服务.

Following are the domain objects in my standalone client application which is making a call to the AResource EJB session bean which I am using as the RESTful webservice.

A.java

@XmlRootElement
public class A implements Serializable{ 

    private List<B> bList = new ArrayList<B>();
    public List<B> getBList() {
        return bList;
    }
    //remaining code

}

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

}

有人可以帮助我了解为什么会发生这种情况以及如何解决此问题吗?

Could someone help me understand why this is happening and how I should solve this problem?

推荐答案

在客户端代码中,您未指定要发送的数据的内容类型-因此Jersey无法找到正确的MessageBodyWritter来序列化b1对象

In your client code you are not specifying the content type of the data you are sending - so Jersey is not able to locate the right MessageBodyWritter to serialize the b1 object.

按如下所示修改main方法的最后一行:

Modify the last line of your main method as follows:

ClientResponse response = resource.type(MediaType.APPLICATION_XML).put(ClientResponse.class, b1);

并在服务器和客户端上将@XmlRootElement批注添加到类B.

And add @XmlRootElement annotation to class B on both the server as well as the client sides.

这篇关于找不到用于Java类型myPackage.B和MIME媒体类型application/octet-stream的消息正文编写器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆