RestEasy - Jax-rs - 在响应正文中发送自定义对象 [英] RestEasy - Jax-rs - Sending custom Object in response body

查看:154
本文介绍了RestEasy - Jax-rs - 在响应正文中发送自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在回复中发送自定义对象。我只想要从我的对象打印的值。

How do I send my custom object in a response. I just want the values printed from my object.

假设我有一个 Person 类型的对象。我试图像这样发送REST响应体。

Lets say I have an object of type Person. I am trying to send in REST response body like this.

  ResponseBuilder response = Response.ok().entity(personObj);
  return response.build();

但我得到 500 错误。
也试过这个:

But I get 500 error. Tried this one too:

  ResponseBuilder response = Response.status(Status.OK).entity(personObj);
  return response.build();

同样的错误。

尝试将内容类型设置为 text / xml 。没用
我在这里想念的是什么?我试过谷歌搜索。但是没有很多例子,尤其是自定义对象;

Tried setting content type as text/xml. No use. What am I missing here? I tried googling. But not many examples out there, especially with the custom objects;

如果我只是将一个字符串传递给 entity()方法。

It returns fine, if I just pass a string to entity() method.

推荐答案

为了从Resteasy资源方法返回数据,你需要做几件事情,具体取决于您要返回的内容。

In order to return data from a Resteasy resource method you need to do several things depending on what you are trying to return.


  • 您需要使用 @Produces <注释您的资源方法/ code>
    注释告诉Resteasy该方法的返回类型应为

  • You need to annotate your resource method with the @Produces annotation to tell Resteasy what the return type of the method should be.

例如,下面的方法返回XML和JSON取决于客户端在 Accept 标头中要求的内容。

For example, the method below returns XML and JSON depending on what the client asks for in their Accept header.


@GET
@Produces({MediaType.APPLICATION_JSON, 
           MediaType.APPLICATION_XML})
public Response foo()
{
     PersonObj obj = new PersonObj();

     //Do something...
     return Response.ok().entity(obj).build();
}


Resteasy支持默认编组以下数据类型:

Resteasy supports marshalling the following datatypes by default:

如果您希望支持的数据类型在此表中,则
表示它们受JAXB支持,您需要做的就是注释
您的 PersonObj 带有JAXB注释的类告诉它如何
marshall和unmarshall对象。

If the datatypes you wish to support are in this table then that means they are supported by JAXB and all you need to do is annotate your PersonObj class with JAXB annotations to tell it how to marshall and unmarshall the object.


@XmlRootElement
@XmlType(propOrder = {"firstName", "lastName"})
public class PersonObj
{
  private String firstName;
  private String lastName;

  //Getters and Setters Removed For Brevity
}







如果你的内容类型不支持开箱即用怎么办?



如果你有一个您想要编组的自定义内容类型,然后您需要创建一个 MessageBodyWriter 实现,它将告诉Resteasy如何编组该类型。


What if your content-type is not supported out of the box?

If you have a custom content-type that you would like to marshall then you need to create a MessageBodyWriter implementation that will tell Resteasy how to marshall the type.

Provider
@Produces({"application/x-mycustomtype"})
public class MyCustomTypeMessageBodyWriter implements MessageBodyWriter {

}

只需实现接口并像其他任何提供商一样注册。

Just implement the interface and register it like any other Provider.

如果您想阅读自定义内容类型,那么您需要实现自定义 MessageBodyReader 来处理传入类型并添加它到接收方法的 @Consumes 注释。

If you would like to read a custom content-type then you need to implement a custom MessageBodyReader to handle the incoming type and add it to the @Consumes annotation on your receiving method.

这篇关于RestEasy - Jax-rs - 在响应正文中发送自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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