- springboot 1.3.5.RELEASE支持velocity模板
springboot 1.5.3.RELEASE不支持velocity模板(推荐使用freemarker/thymeleaf)
- springboot返回json不需要自己再次处理了,只需要在你的Controller上加上@RestController注解就行了,springboot会自动帮你转换为json
- springboot 引入fastjson
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.21</version>
</dependency>
@Configuration
public class MyFastJsonConfig {
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters(){
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastJsonConfig.setCharset(Charset.forName("UTF-8"));
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
}
Spring Boot使用FastJson解析JSON数据
- 参数传递
Spring Boot Web项目之参数绑定
使用@RequestParam(value="")来改变参数名字
使用@RequestParam(defaultValue=""),不传参时,使用默认值
使用@RequestParam(required=true),强制必须传参数
public Result test3Id(@PathVariable("id") long id)
public Result test4(int p1,int p2)
public Result test5(@RequestParam(value="p1",defaultValue="12",required=false) Integer pp)
spring boot 学习笔记(005)提交json对象
function doajax(url1,data1,callback) {
$.ajax({
type: "POST",
url: url1,
data: data1,
dataType: "json",
contentType : "application/json",
success: function(data){
console.log(data);
callback(data);
}
})
}
function test6() {
var url1="http://localhost:8097/test6";
var data1="{\"name\":\"xiaomi\",\"age\":\"22\"}";
doajax(url1,data1,function(data){
});
}
@RequestMapping(value="/test6")
public Result test6(@RequestBody User user){
Result rs = new Result();
User u=new User();
u.setAge(user.getAge());
u.setName(user.getName());
rs.setData(u);
return rs;
}