springboot-web

  1. springboot 1.3.5.RELEASE支持velocity模板
    springboot 1.5.3.RELEASE不支持velocity模板(推薦使用freemarker/thymeleaf)
  2. springboot返回json不需要自己再次處理了,只需要在你的Controller上加上@RestController注解就行了,springboot會自動幫你轉換為json
  3. 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數據

  1. 參數傳遞
    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;
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容