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);
}
}