内容简介:最近在对接支付宝,在编写支付需要的bean的时候,发现支付宝需要传递的参数命名方式都是下划线,自己idea又装了阿里巴巴的编程规约,代码老是有黄色提示,显得特别难看。于是乎我把属性改成了驼峰的命名方式,然后下意识的在上面加了一个@JsonProperty注解实现相互转换过程中属性命名方式也能自动转换。可是我运行代码时发现代码报错运行不起来了。主要代码如下:突然发现这个@JsonProperty注解是Spring框架自带jackson的注解,不是阿里FastJson的注解,于是乎我看了看找到了下面的这个注解
最近在对接支付宝,在编写支付需要的bean的时候,发现支付宝需要传递的参数命名方式都是下划线,自己idea又装了阿里巴巴的编程规约,代码老是有黄色提示,显得特别难看。于是乎我把属性改成了驼峰的命名方式,然后下意识的在上面加了一个@JsonProperty注解实现相互转换过程中属性命名方式也能自动转换。可是我运行代码时发现代码报错运行不起来了。主要代码如下:
public class AliPayParam {
@JsonProperty(name="out_trade_no")
private String outTradeNo;
@JsonProperty(name="total_amount")
private String totalAmount;
/**
* 公共回传参数
*/
@JsonProperty(name="passback_params")
private String passbackParams;
private String subject;
private String body;
@JsonProperty(name="product_code")
private String productCode;
/**
* 该参数在请求到支付宝时开始计时,该笔订单允许的最晚付款时间,逾期将关闭交易。
* 取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天
* (1c-当天的情况下,无论交易何时创建,都在0点关闭)。
* 该参数数值不接受小数点, 如 1.5h,可转换为 90m。
*/
@JsonProperty(name="timeout_express")
private String timeoutExpress;
}
突然发现这个@JsonProperty注解是Spring框架自带jackson的注解,不是阿里FastJson的注解,于是乎我看了看找到了下面的这个注解:
public @interface JSONField {
/**
* config encode/decode ordinal
* @since 1.1.42
* @return
*/
// 配置序列化和反序列化的顺序,1.1.42版本之后才支持
int ordinal() default 0;
// 指定字段的名称
String name() default "";
// 指定字段的格式,对日期格式有用
String format() default "";
// 是否序列化
boolean serialize() default true;
// 是否反序列化
boolean deserialize() default true;
SerializerFeature[] serialzeFeatures() default {};
Feature[] parseFeatures() default {};
String label() default "";
/**
* @since 1.2.12
*/
boolean jsonDirect() default false;
/**
* Serializer class to use for serializing associated value.
*
* @since 1.2.16
*/
Class<?> serializeUsing() default Void.class;
/**
* Deserializer class to use for deserializing associated value.
*
* @since 1.2.16
*/
Class<?> deserializeUsing() default Void.class;
/**
* @since 1.2.21
* @return the alternative names of the field when it is deserialized
*/
String[] alternateNames() default {};
/**
* @since 1.2.31
*/
boolean unwrapped() default false;
}
知道这个注解之后,把@JsonProperty替换成@JSONField注解就行了
最后更新于 2018-12-24 10:04:03 并被添加「java fastjson」标签,已有 2 位童鞋阅读过。
以上所述就是小编给大家介绍的《使用FastJson进行对象和JSON转换属性命名规则为下划线和驼峰的问题》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- zorm 1.4.8 发布,优化字段名称驼峰映射
- MyBatis Map结果的Key转为驼峰式2
- Go语言 “ _ ”(下划线)
- 文字悬停下划线动画效果集合
- golang之下划线(_)之语义说明
- Swoole 5 将移除 PSR-0 下划线风格类名
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Introduction to the Design and Analysis of Algorithms
Anany Levitin / Addison Wesley / 2006-2-24 / USD 122.00
Based on a Based on a new classification of algorithm design techniques and a clear delineation of analysis methods, "Introduction to the Design and Analysis of Algorithms" presents the subject in a c......一起来看看 《Introduction to the Design and Analysis of Algorithms》 这本书的介绍吧!