java RestEasy - Jax-rs - 在响应正文中发送自定义对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17533280/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:13:57  来源:igfitidea点击:

RestEasy - Jax-rs - Sending custom Object in response body

javarestjax-rsresponseresteasy

提问by Kevin Rave

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

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

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

假设我有一个类型的对象Person。我正在尝试像这样发送 REST 响应正文。

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

But I get 500error. Tried this one too:

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

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

Same error.

同样的错误。

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;

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

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

如果我只是将字符串传递给entity()方法,它会返回正常。

回答by gregwhitaker

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

为了从 Resteasy 资源方法返回数据,您需要根据要返回的内容做几件事。

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

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

  • 您需要使用注释来注释您的资源方法,@Produces以告诉 Resteasy 方法的返回类型应该是什么。

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

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

     //Do something...
     return Response.ok().entity(obj).build();
}
@GET
@Produces({MediaType.APPLICATION_JSON, 
           MediaType.APPLICATION_XML})
public Response foo()
{
     PersonObj obj = new PersonObj();

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

Resteasy supports marshalling the following datatypes by default:

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

enter image description here

在此处输入图片说明

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 PersonObjclass with JAXB annotations to tell it how to marshall and unmarshall the object.

如果您希望支持的数据类型在此表中,那么这意味着 JAXB 支持它们,您需要做的就是PersonObj使用 JAXB 注释对您的类进行注释,以告诉它如何编组和解组对象。

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

  //Getters and Setters Removed For Brevity
}
@XmlRootElement
@XmlType(propOrder = {"firstName", "lastName"})
public class PersonObj
{
  private String firstName;
  private String lastName;

  //Getters and Setters Removed For Brevity
}


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 MessageBodyWriterimplementation that will tell Resteasy how to marshall the type.

如果您有一个想要编组的自定义内容类型,那么您需要创建一个MessageBodyWriter实现来告诉 Resteasy 如何编组该类型。

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

}

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

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

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

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