Web services 在JAX-RS中从pojo生成自定义响应

Web services 在JAX-RS中从pojo生成自定义响应,web-services,jax-rs,Web Services,Jax Rs,我有如下要求 Emp{ String id; String name; } Employee emp = (Object from service) builder.entity(emp); 但在将response构建为JSON时,我希望将其生成为 { employee_id : val, full_name : val, //custom attribute in pojo i wanted to inject company_url : xxx.com } 在这里,我没有权

我有如下要求

Emp{
 String id;
 String name;
}

Employee emp = (Object from service)
builder.entity(emp);
但在将response构建为JSON时,我希望将其生成为

{
 employee_id : val,
 full_name : val,
 //custom attribute in pojo i wanted to inject
 company_url : xxx.com
}

在这里,我没有权限让Emp类添加属性来使用注释更改JSON元素名称。

您可以创建一个用JAXB注释的
EmpDTO
类,在资源方法中实例化该类并返回它(如您所解释的,包含您需要的所有额外数据),然后JAX-RS实现将DTO转换为JSON消息

或者您可以制作自己的
MessageBodyWriter
(用
@Producer
+
@products(“application/json”)
注释)并处理
Emp
类并生成json消息(例如使用json-PAPI)

嗯。 泽维尔