找不到 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

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

问题描述

我是 RESTful webservices 的新手,并试图从独立的客户端应用程序更新我的 @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.

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

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 是一个 EJB 会话 bean,我将其用作 RESTful Web 服务.

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.

以下是我的独立客户端应用程序中的域对象,它正在调用我用作 RESTful web 服务的 AResource EJB 会话 bean.

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

并在服务器端和客户端向 B 类添加 @XmlRootElement 注解.

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天全站免登陆