zoukankan      html  css  js  c++  java
  • json注解及序列化

    一、json框架

    市面上的json框架常用的有 jackson、gson、fastjson。大家比较推崇的是fastjson,但是springmvc默认集成的是 jackson。

    在一个项目中建议一个项目只是用一种 json 框架。

    二、json使用场景

    1、mvc

    http传输的body虽然是json格式的,但实际还是以串字符串。我们使用对象接收需要进行json转换。同样,返回给浏览器时在解析成字json格式的符串。

    springmvc 支持多种 json框架,默认jsckson,想要修改jackson。

    step1、排除 jackson

    step2、引入 fastjson

    step3、重写 MappingJackson2HttpMessageConverter,HttpMessageConverter。

    2、远程http调用

    有时返回结果是String需要转换

    3、redis序列化

    三、注解

    1、注意json注解的使用要和提供注解的json框架对上。比如@JsonProperty("companId") 是jackson提供的,@JSONField(name = "companId") 是 fastjson提供的。如果springmvc 使用的框架是 jackson,此时在字段上增加 @JSONField 是无效的。

    2、jsckson注解

    @JacksonAnnotation
    @JacksonAnnotationsInside
    @JacksonInject
    @JsonAlias
    @JsonAnyGetter
    @JsonAnySetter
    @JsonAutoDetect
    @JsonBackReference
    @JsonClassDescription
    @JsonCreator
    @JsonEnumDefaultValue
    @JsonFilter
    @JsonFormat
    @JsonGetter
    @JsonIdentityInfo
    @JsonIdentityReference
    @JsonIgnore
    @JsonIgnoreProperties
    @JsonIgnoreType
    @JsonInclude
    @JsonManagedReference
    @JsonMerge
    @JsonProperty
    @JsonPropertyDescription
    @JsonPropertyOrder
    @JsonRawValue
    @JsonRootName
    @JsonSetter
    @JsonSubTypes
    @JsonTypeId
    @JsonTypeInfo
    @JsonTypeName
    @JsonUnwrapped
    @JsonValue
    @JsonView

    3、fastjson注解

    @JSONField(name = "companId")
    @JSONCreator
    @JSONPOJOBuilder
    @JSONType
    @FastJsonFilter()
    @FastJsonView
    @ResponseJSONP

    4、顺便说一下java提供的字段校验注解:都来自package javax.validation.constraints;包

    @AssertFalse
    @AssertTrue
    @DecimalMax(value = "")
    @DecimalMin(value = "")
    @Digits(integer = 6, fraction = 2, message = "{javax.validation.constraints.Digits.message}")
    @Email
    @Future
    @FutureOrPresent
    @Max(1)
    @Min(1)
    @Negative
    @NegativeOrZero
    @NotBlank
    @NotEmpty
    @NotNull
    @Null
    @Past
    @PastOrPresent
    @Pattern(regexp = "\w+$")
    @Positive
    @PositiveOrZero
    @Size

    5、
    package org.springframework.data.annotation;
    @Transient
    @TypeAlias
    @AccessType
    @AccessType
    @Id
    @Immutable
    @Persistent
    @QueryAnnotation
    @ReadOnlyProperty
    @Reference
    @Version

     https://www.cnblogs.com/panbingqi/p/11212194.html

    https://www.cnblogs.com/yucy/p/9057049.html



  • 相关阅读:
    openwrt官方固件怎么中继网络
    hibernate 映射一对多
    jsp连接access数据库
    表单判断值是否为空
    eclipse java ee jsp tomcat Server文件夹给删了,怎么办?
    Provided id of the wrong type for class pojo.Books. Expected: class java.lang.Integer, got class java.lang.Long
    myeclipse创建hibernate工程
    Caused by: java.sql.SQLException: Field 'id' doesn't have a default value
    Mac 挂载树莓派nfs
    树莓派集群实践——nfs
  • 原文地址:https://www.cnblogs.com/cuiqq/p/12082563.html
Copyright © 2011-2022 走看看