gpt4 book ai didi

Java REST API 从字符串返回 JSON

转载 作者:太空宇宙 更新时间:2023-11-04 13:25:21 25 4
gpt4 key购买 nike

我的数据库中存储了一个 JSON,我想返回该 json,因为它在 jax-rs get 服务中,而不使用 POJO。有什么办法可以做到这一点吗?我尝试将其设置为字符串,但结果被转义。我还尝试返回 JSONObject,但得到“org.codehaus.jackson.map.JsonMappingException:找不到类 org.json.JSONObject 的序列化程序”,所以我想我无法使用该对象类型。最后我使用了 JSONNode,它返回了我的数据,如下所示:

{
"nodeType": "OBJECT",
"int": false,
"object": true,
"valueNode": false,
"missingNode": false,
"containerNode": true,
"pojo": false,
"number": false,
"integralNumber": false,
"floatingPointNumber": false,
"short": false,
"long": false,
"double": false,
"bigDecimal": false,
"bigInteger": false,
"textual": false,
"boolean": false,
"binary": false,
"null": false,
"float": false,
"array": false
}

代码。

@GET
@Path("/campanas")
public Response obtenerCampanas(@HeaderParam("Authorization") String sessionId) {
ResponseBase response = new ResponseBase();
int requestStatus = 200;
CampanaResponse campanaResponse = campanasFacade.obtenerCampanas();
response.setData(campanaResponse);
response.setRequestInfo(GlosaCodigoRequest.OPERACION_EXITOSA);
return Response.status(requestStatus).entity(response).build();
}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Campanas")
public class CampanaResponse implements Serializable {
private static final long serialVersionUID = -7414170846816649055L;
@XmlElement(name = "campanas", required = true)
private List<Campana> campanas;
@XmlElement(name = "fecha", required = true)
private Date fecha;

//getters.. setters

public static class Campana {
private String idCampana;
private String nombre;
private String urlBanner;
private String global;
private String numeroCuenta;
private Date fechaDonaciones;
private Date fechaInicio;
private Date fechaFin;
private JSONObject config;

//getters..setters
}
}

有什么办法可以做到这一点吗?谢谢。

jax-rs,weblogic 12.1.3

最佳答案

我有类似的需求,但我所做的只是将其从Gson序列化到实体以在内部处理它,当打印或保存它时,只需将其序列化回GSON,逻辑是这样的(顺便说一句,我两种方式都使用了Gson):

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.data.repository;
import com.data.Entity;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

@Path("/rest")
public class RestService {
/*
... Other calls
*/
private String toJson(Object entity) {
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS zzz")
.setPrettyPrinting()
.create();
String result = gson.toJson(entity);
return result.replace("\\\"", "");
}

@GET
@Path("/{param1}/{param2}")
@Produces({"application/xml", "application/json", "text/plain", "text/html"})
public Response getEntity(@PathParam("param1") String param1,
@PathParam("param2") String param2) {
Entity entity = repository.getEntity(param1, param2);
if (entity == null) {
return Response
.status(Response.Status.NOT_FOUND)
.entity(
String.format(
"param1 %s does not have a valid record for param2 %s",
param1, param2))
.build();
}
return Response.ok(this.toJson(entity)).build();
}
}

这里是实体:

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

import java.util.Date;

public class Entity {

@SerializedName("Field1")
private String field1;

@SerializedName("Field2")
private int field2;

@SerializedName("Field3")
private int field3;

@SerializedName("Field4")
private Date field4;

public Entity() {
}

/*
...
... Gets and Sets
...
*/

@Override
public String toString() {
Gson gson = new Gson();
String json = gson.toJson(this, Entity.class);
return json;
}
}

获取 json 并将其转换为实体的逻辑如下所示:

Gson gson = new Gson();
Entity repoSequence = gson.fromJson(jsonString, Entity.class);

关于Java REST API 从字符串返回 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32726207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com