gpt4 book ai didi

java - JSON 响应使用 Jackson 和 JAX-RS 异常映射器转义了引号

转载 作者:行者123 更新时间:2023-12-02 09:52:42 25 4
gpt4 key购买 nike

我有一个简单的要求,如果应用程序遇到异常,我的 JAX-RS Rest 端点应返回带有 500 HTTP header 状态的自定义 JSON 响应。

构建响应所需的数据来自具有多个属性的对象(见下文)。问题是,我只对每个属性中的一两个值感兴趣(几十个)。而且我无法修改任何这些模型/类(有些模型/类具有用于 JSON 处理的 Jackson 注释,例如,在序列化期间应丢弃 null 属性)。

public class MainObject {
private FirstProperty firstProperty;
private SecondProperty secondProperty;
private ThirdProperty thirdProperty;
// other codes
public String toString() {
ObjectMapper mapper = new ObjectMapper();
try { return mapper.writeValueAsString(this); }
catch (Exception e) { return null; }
}
}

public class FirstProperty {
private boolean bol = true;
private double dob = 5.0;
private List<String> subProperty;
// other properties
public String toString() {
ObjectMapper mapper = new ObjectMapper();
try { return mapper.writeValueAsString(this); }
catch (Exception e) { return null; }
}
}

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class SecondProperty {
private String str;
private List<String> subProperty;
// other properties
public String toString() {
ObjectMapper mapper = new ObjectMapper();
try { return mapper.writeValueAsString(this); }
catch (Exception e) { return null; }
}
}

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class ThirdProperty {
private int intProp = true;
private List<String> subProperty;
// other properties
public String toString() {
ObjectMapper mapper = new ObjectMapper();
try { return mapper.writeValueAsString(this); }
catch (Exception e) { return null; }
}
}

我应该看到返回的预期 JSON 位于客户端(例如,浏览器 - 在 Edge 中测试):

{
"firstProperty" : { "subProperty" : [ "val1" ] },
"secondProperty" : { "str" : "val2", "subproperty" : [ "val3", "val6" ] },
"thirdProperty" : { "subProperty" : [ "val4" ] }
}

相反,我的所有字段名称及其值都转义了引号,并在整个值周围添加了额外的双引号,例如:

{
"firstProperty" : "{ \"subProperty\" : [ \"val1\" ] }",
"secondProperty" : "{ \"str\" : \"val2\", \"subproperty\" : [ \"val3\", \"val6\" ] }",
"thirdProperty" : "{ \"subProperty\" : [ \"val4\" ] }"
}

请注意额外的"大括号之前和之后。我的环境是:

Java 1.8.45
FasterXML Jackson 2.9.8
Spring Boot 2.0.1
RestEasy (JBoss) JAX-RS
JBoss 6.4

我消除了代码中的大部分“噪音”,看看什么时候会发生这种情况。这是 Controller :

@Path("/")
public class MainController {

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/rest/path")
public MainObject getMainObject throws MyCustomException {
// A service call that throws MyCustomException
}
}

和 JAX-RS ExceptionMapper我在哪里发回响应:

@Provider
public class MyCustomExceptionMapper extends ExceptionMapper<MyCustomException> {

@Override
public Response toResponse(MyCustomException ex) {
HashMap<String, Object> responseBody = new HashMap<>();

String strEx = ex.getStrEx(); // Comes from SecondProperty.str stored in MyCustomException, not that it matters

// Instantiate an empty object that contains
MainObject obj = new MainObject();
obj.getFirstProperty().setSubProperty(ex.getStrs());
obj.getSecondProperty().setStr(strEx);
obj.getSecondProperty().setSubProperty(ex.getStrs());
obj.getThirdProperty().setSubProperty(ex.getStrs());

responseBody.put("firstProperty", serializeFirstProperty(obj.getFirstProperty()));
responseBody.put("secondProperty", serializeSecondProperty(obj.getSecondProperty()));
responseBody.put("thirdProperty", serializeThirdProperty(obj.getThirdProperty()));

Response response = Response.status(/* 500 status */).entity(responseBody).build();

return response;
}

}

由于我只需要从每种类型发回整体属性的一小部分,因此我创建了一个自定义 StdSerializer这只会填充所需的属性。为了简洁起见,我只做 serializeFirstProperty()但它们或多或少都是相同的:

private StdSerializer<FirstProperty> getFPSerializer(FirstProperty firstProperty) {
return new StdSerializer<FirstProperty>(FirstProperty.class) {
@Override
public void serialize(FirstProperty value, JsonGenerator gen, SerializerProvider provider) throws IOException {

gen.writeStartObject();
if (/* there are items in FirstProperty.subProperty */) {
gen.writeArrayFieldStart("subProperty");
for (String str : value.getSubProperty()) {
gen.writeString(str);
}
gen.writeEndArray();
}
gen.writeEndObject();
}
}

private <T> ObjectMapper getCustomOM(StdSerializer<?> serializer) {
ObjectMapper mapper = new ObjectMapper();

SimpleModule sm = new SimpleModule();
sm.addSerializer(serializer);
mapper.registerModule(module);

return mapper;
}

然后使用这些辅助方法,例如:

private String serializeFirstProperty(FirstProperty firstProperty) {
ObjectMapper mapper = getCustomOM(getFPSerializer(firstProperty));

String ser = null;
try { ser = mapper.writeValueAsString(firstProperty); }
catch (JsonProcessingException e) { return null; }
return ser;
}

我已经尝试了无数种 ObjectMapper 的配置,例如disable(JsonParser.Feature.ALLOW_BACKLASH_ESCAPING_ANY_CHARACTER) (找不到 JsonGenerator 的任何相关标志,我真的想以类似的方式禁用它)。

或者显式返回 Object来自serializeFirstProperty() ,或替换所有 \""serializeFirstProperty()ser已返回。

或设置自定义StdSerializerJsonGenerator.setCharacterEscapes(new CharacterEscapes() { //... }或者使用 JAX-RS Response无济于事。我似乎总是得到带引号的“字符串”值,例如:

"firstProperty" : "{ \"subProperty\" : [ \"val1\" ] }"

如果我只是这样做

responseBody.put("firstProperty", mapper.writeValueAsString(obj.getFirstProperty()));

不知何故,这会产生正确的 JSON 输出,但是,它包含许多不必要的属性,在这种异常处理情况下我不想要这些属性。

有趣的是,当我查看response时(或 responseBody map ),一切看起来都正确(我没有看到带有双引号的值)。

还请注意,我不仅无法修改模型,而且它们的一些属性在创建过程中会使用默认值进行实例化,因此非空包含不起作用,如果我这样做,它们将出现在最终的 JSON 中不要使用自定义序列化。

有谁知道是什么导致了这种转义和额外的引号?

最佳答案

我想我在第一次尝试回答这个问题时误解了这个问题。

问题是您将属性序列化为字符串(使用mapper.writeValueAsString(this)),然后将其添加到您认为是字符串的responseBody< strong>json 对象 映射,但它是一个 Java 对象 映射的字符串。在运行时的情况下,它是映射到另一个字符串的字符串(序列化的 json 对象表示为 Java 字符串) Java 字符串也是 Java 对象。

您想要做的是构造一个 Java 对象 responseBody 而不是 map 。它应该充当具有所有特定属性等的 DTO,然后使用映射器在一个操作中将其序列化。因为如果您首先将属性序列化为 json 字符串,那么从 Java 的角度来看它只是一个字符串,映射器没有机会将其解释为 json 对象。

关于java - JSON 响应使用 Jackson 和 JAX-RS 异常映射器转义了引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56204116/

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