注解使用记录

1.@JsonProperty

@JsonProperty("n")
private String name;
	
@JsonProperty("u")
private String url;

说明:转换成json对象时,key值会改成属性里设置的值。需要jar为 jackson-annotations-2.4.0.jar


2.获得HttpServletRequest对象

@Autowired  
private HttpServletRequest request;

3.注解读取properties文件属性值

在Spring中读取配置文件:
<context:property-placeholder location="classpath:resource/resource.properties" />

在java中使用注解声明,对属性进行值注入
@Value("${FTP_ADDRESS}")
private String FTP_ADDRESS;

4.@RequestParam

用在Controller层参数列表中,用于设置参数默认值,参数为空时使用defaultValue值
注意:该注解,required值默认为true;required只检测参数是否传递,不检测参数传的是否为空值
@RequestParam(defaultValue = "1") Integer page ...


日期格式化
@RequestParam(value = "startUseDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startUseDate

5.@JsonSerialize

用于model中,对Date类型数据进行输出格式化(如显示到前端)
...
@JsonSerialize(using=JsonDateSerializer.class)
public Date getDate() {
    return date;
}
...

@Component
public class JsonDateSerializer extends JsonSerializer<Date>{

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");

    @Override
    public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
            throws IOException, JsonProcessingException {

        String formattedDate = dateFormat.format(date);

        gen.writeString(formattedDate);
    }

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值