本文介绍了如何从JsonSchema中删除引号的反斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要从字符串中删除id属性。
我得到了用于转义字符的奇怪的额外反斜杠。在调试和执行mapper.WriteValueAsString(架构)时,我没有看到这一点。但是,在我设置为Property之后,我看到了反斜杠和额外的引号。
您可以使用MixIn功能指示Jackson删除id。查看example。
对象被序列化两次,第一次是由您在控制器方法中直接进行的,第二次是Spring。
在您的示例中,您需要:
在Setting类中使用@JsonRawValue批注:
class Setting {
String configurationName;
@JsonRawValue
String configuration;
}
我有一节课:
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" } } }" }上述输出的问题:
您知道如何解决这些问题吗?
推荐答案创建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中删除引号的反斜杠?
发布评论