http請求 url傳遞參數

1、參數帶有斜槓

方法1:修改一下啓動類,加一個系統參數,重寫WebMvcConfigurerAdapter的configurePathMatch方法

@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter {

    public static void main(String[] args) throws Exception {
        System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true");
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setUrlDecode(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }
}

參考:Encoded slash (%2F) with Spring RequestMapping path param gives HTTP 400

方法2:

傳參方式由URL變量改爲Request參數。

2、設置參數非必填

ps:@RequestParam(required=false) int XXX 取參數的時候,當參數沒有的時候Spring默認賦值爲空。而此時使用基本類型int,所以報錯,建議使用包裝類 Integer。


@ResponseBody
@RequestMapping(value = {"/findGroupByGroupName/{batchNo}/{groupSex}"}, method = RequestMethod.GET)
@ApiOperation(value = "模糊搜索查詢分組情況是否成功", notes = "輸入分組的批次,分組的性別,分組的部分名稱", tags = {"查詢"})
public ItooResult findGroupByGroupName(
    @ApiParam(name = "batchNo", value = "批次", required = true) @PathVariable String batchNo,
    @ApiParam(name = "groupSex", value = "性別", required = true) @PathVariable String groupSex,
    @ApiParam(name = "groupName", value = "名稱") @RequestParam(required = false) String groupName) {
}

3、參數綁定註解

@PathVariable 

URL變量

當使用請求URL中的模板變量樣式映射時, 即 someUrl/{paramId}, 這時的paramId可通過 @Pathvariable註解綁定它傳過來的值到方法的參數上。


@RequestMapping("/pets/{petId}")
public void findPet(@PathVariable String ownerId, @PathVariable String petId)

@RequestParam

用來處理Content-Type: 爲 application/x-www-form-urlencoded編碼的內容,提交方式GET、POST

常用來處理簡單類型的綁定,通過Request.getParameter() 獲取的String可直接轉換爲簡單類型的情況( String--> 簡單類型的轉換操作由ConversionService配置的轉換器來完成);

因爲使用request.getParameter()方式獲取參數,所以可以處理get 方式中queryString的值,也可以處理post方式中 body data的值;

@RequestMapping(method = RequestMethod.GET)
public String setupForm(@RequestParam("petId") int petId, ModelMap model)

@RequestBody

該註解常用來處理Content-Type: 不是application/x-www-form-urlencoded編碼的內容,例如application/json, application/xml等;


@RequestMapping(value = "/something", method = RequestMethod.PUT)
public void handle(@RequestBody String body, Writer writer) 

@RequestHeader

可以把Request請求header部分的值綁定到方法的參數上。


@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding,
                              @RequestHeader("Keep-Alive") long keepAlive)

@CookieValue

可以把Request header中關於cookie的值綁定到方法的參數上。


@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@CookieValue("JSESSIONID") String cookie)

@SessionAttributes

該註解用來綁定HttpSession中的attribute對象的值,便於在方法中的參數裏使用。

該註解有value、types兩個屬性,可以通過名字和類型指定要使用的attribute 對象;

@RequestMapping("/editPet.do")
@SessionAttributes("pet")
public class EditPetForm 

@ModelAttribute

用於方法上時:  通常用來在處理@RequestMapping之前,爲請求綁定需要從後臺查詢的model;


@ModelAttribute
public Account addAccount(@RequestParam String number)

ps:調用@RequestMapping的方法之前,爲request對象的model裏put(“account”, Account);

用於參數上時: 用來通過名稱對應,把相應名稱的值綁定到註解的參數bean上;要綁定的值來源於:

A) @SessionAttributes 啓用的attribute 對象上;

B) @ModelAttribute 用於方法上時指定的model對象;

C) 上述兩種情況都沒有時,new一個需要綁定的bean對象,然後把request中按名稱對應的方式把值綁定到bean中。

@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@ModelAttribute Pet pet) 

ps:首先查詢@SessionAttributes有無綁定的Pet對象,若沒有則查詢@ModelAttribute方法層面上是否綁定了Pet對象,若沒有則將URI template中的值按對應的名稱綁定到Pet對象的各屬性上。

參考:參數綁定註解詳解

 


 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章