如何从JsonSchema中删除引号的反斜杠?

编程入门 行业动态 更新时间:2024-10-15 00:23:31
本文介绍了如何从JsonSchema中删除引号的反斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一节课:

class Setting { String configurationName; String configuration; }

我想返回配置的字符串表示形式。根据某些条件,这可以是不同的对象。

在以下一项服务中:

@GET @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response getConfigurationSetting () { try { Setting settingPojo = new Setting(); settingPojo.setCOnfigurationName("DataBase"); ObjectMapper mapper = new ObjectMapper(); SchemaFactoryWrapper visitor = new SchemaFactoryWrapper(); mapper.acceptJsonFormatVisitor(OracleConfiguration.class, visitor); JsonSchema schema = visitor.finalSchema(); settingPojo.setConfiguraton(mapper.writeValueAsString(schema)); return Response.status(HTTP.CodeOK).entity(settingPojo).build(); } catch (Exception ex) { // exception logic } }

可以有如下所示的不同类:";MySQLConfiguration.class";。

示例:

{ configurationName : "DataBase", configuration: "{ "type":"object", "id":"urn":"jsonschema":"database":"model":"OracleConfiguration", "properties":{ "numberOfConnection":{ "type":"integer" }, "connectionDate":{ "type":"integer", "format":"utc-millisec" }, "isconnected":{ "type":"boolean" } } }" }

上述输出的问题:

  • 我要从字符串中删除id属性。
  • 我得到了用于转义字符的奇怪的额外反斜杠。在调试和执行mapper.WriteValueAsString(架构)时,我没有看到这一点。但是,在我设置为Property之后,我看到了反斜杠和额外的引号。
  • 您知道如何解决这些问题吗?

    推荐答案
  • 您可以使用MixIn功能指示Jackson删除id。查看example。
  • 对象被序列化两次,第一次是由您在控制器方法中直接进行的,第二次是Spring。
  • 在您的示例中,您需要: 在Setting类中使用@JsonRawValue批注:

    class Setting { String configurationName; @JsonRawValue String configuration; }

    创建MixInabstract class:

    abstract class JsonSchemaWithoutId { @JsonIgnore public String id; @JsonIgnore public abstract String getId(); }

    简单用法:

    import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonRawValue; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.module.jsonSchema.JsonSchema; import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper; import lombok.Data; import java.io.File; import java.io.IOException; public class JsonMixInAndSchemaApp { public static void main(String[] args) throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.enable(SerializationFeature.INDENT_OUTPUT); mapper.addMixIn(JsonSchema.class, JsonSchemaWithoutId.class); Setting settingPojo = new Setting(); settingPojo.setConfigurationName("Setting"); SchemaFactoryWrapper visitor = new SchemaFactoryWrapper(); mapper.acceptJsonFormatVisitor(Setting.class, visitor); JsonSchema schema = visitor.finalSchema(); settingPojo.setConfiguration(mapper.writeValueAsString(schema)); mapper.writeValue(System.out, settingPojo); } }

    上面的代码打印

    { "configurationName" : "Setting", "configuration" : { "type" : "object", "properties" : { "configurationName" : { "type" : "string" }, "configuration" : { "type" : "string" } } } }

    更多推荐

    如何从JsonSchema中删除引号的反斜杠?

    本文发布于:2023-11-06 08:11:46,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1563156.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:斜杠   引号   JsonSchema

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!