url http request to pass parameters

1, the parameter by a slash

Method 1: modify the startup class, plus a system parameter, configurePathMatch method of rewriting WebMvcConfigurerAdapter

@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

Method 2:

By the way parameter passing variables to Request URL parameters.

2, set the parameters optional

ps: @RequestParam (required = false) int time taken XXX parameter when the parameter is not empty Spring implicitly assigned. At a time when using basic type int, so the error, it is recommended to use the wrapper class 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, parameter binding annotations

@PathVariable 

URL variables

When the template variables in the request URL style mapping, i.e. someUrl / {paramId}, at this time may be bound by @Pathvariable paramId annotations on process parameter values ​​to pass over it.


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

@RequestParam

For processing Content-Type: application of the content / x-www-form-urlencoded encoded submission GET, POST

Common binding types simple to handle, by Request.getParameter () String obtained can be converted directly to the case of a simple type (String -> a simple type of switching operation arranged by the converter to complete ConversionService);

Since use request.getParameter () mode acquisition parameters, it is possible to get processed value queryString embodiment, the embodiment may be post processed value of the body data;

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

@RequestBody

The annotation process used to Content-Type: not application/x-www-form-urlencodedencoded contents, for example, application / json, application / xml like;


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

@RequestHeader

The parameter value can be requested Request header portion bound to the method.


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

@CookieValue

Request header value can be bound to a cookie on the method parameters.


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

@SessionAttributes

The annotation for the attribute value of the object HttpSession bindings, easy to use in the process parameters in.

The notes have value, types two properties, you can specify an attribute object to be used by name and type;

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

@ModelAttribute

When used on a method: before processing usually used @RequestMapping, model binding need, request from the background;


@ModelAttribute
public Account addAccount(@RequestParam String number)

ps: before method call @RequestMapping of, model for the request object's put ( "account", Account);

When the parameters used: by name to a corresponding, binds the value corresponding to the parameter name annotation bean; values ​​to be bound from:

On A) @SessionAttributes enabled attribute object;

B) @ModelAttribute model object for specified when the method;

C) above two cases are sometimes no, a new new bean objects to be bound, and the request by name in a corresponding manner to bind the values ​​to the bean.

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

ps: Pet objects @SessionAttributes first queries whether the binding of, if not the query on whether the method level @ModelAttribute bound Pet objects, if there is no value will be URI template in accordance with the corresponding name of each object bound to the Pet on the property.

Reference: parameter binding Detailed notes

 


 

 

 

 

Published 49 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/csdnzhang365/article/details/103445803