简体   繁体   English

java POJO中作为字符串嵌入的json对象

[英]Embedded json object as String in java POJO

My service receive the following request:我的服务收到以下请求:

@Data
public class InputRequest{
private Information info;
private String response;
}

I am trying to figure out what is the right way to represent the above in the form of JSON.我试图弄清楚以 JSON 的形式表示上述内容的正确方法是什么。

Note: response can be any jsonObject like注意: response可以是任何 jsonObject,例如

{
"country":"Canada",
"State":"Ontatio"
}

Hoping service you are referring to Rest service.希望service您指的是休息服务。 If you are purely building using JAXRS specifications.如果您纯粹是使用 JAXRS 规范进行构建。 Then it can be done easily by converting any Pojo to JSON using javax.ws.rs.core.Response API.然后可以通过使用javax.ws.rs.core.Response API 将任何 Pojo 转换为 JSON 来轻松完成。

For instance, here MessageData is returning by Response will convert by JSON underlying container.例如,这里由 Response 返回的 MessageData 将由 JSON 底层容器转换。

// pojo web service will return to service consumer
MessageData response = new MessageData();
response.setMessageId(msgId);

// return pojo with response code. 
Response.status(Response.Status.CREATED).entity(response).build();

I've recently written an article on creating and testing REST service using JAX RS specification with Apache TomEE.我最近写了一篇关于使用 JAX RS 规范和 Apache TomEE 创建和测试 REST 服务的文章。 See if that can be useful for you.看看这对你是否有用。

@Data
public class InputRequest{
private Information info;
private Object response;
}

Unless there is some need to work with the string directly later if you map it to just an Object then it can work for both any single Object (including a String) or a multi-item Object like a List<> or Map<> or ....除非稍后需要直接使用字符串,如果您将它映射到一个对象,那么它可以适用于任何单个对象(包括字符串)或多项目对象,如 List<> 或 Map<> 或……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM