Restful parsing posf method to pass parameters

Conclusion:

restful-style interfaces do not support multiple parameters

Note : This article refers to the case of serializing parameters through json
1. Prefix

a MyParam class defined for testing

public class MyParam {
    private String str;
    private Integer integer;
    // Omit getters and setters...
    }


What I'm testing is using the Chrome plug-in Advanced REST client, which can simulate the browser to send various requests and customize the header and body.
When testing, you need to use the post method and add it to the http request header

accept: application/json
content-type: application/json


Then in the Body of the htpp request, enter the parameters in json format, such as {"str":"bb","integer":3}.

The following are several forms of the multi-parameter interface, as well as the input parameters, and the results of the parsing.

2. The first type: two String parameters

@POST
@Path("demo")
public Result function(String param1, String param2);


Incoming parameters:
{"param1":"bb","param2":"cc"} 
Parsed parameters:

param1: "{"param1":"bb","param2":"cc"}"
param2: ""


In this style, when the transmitted parameters are read, the inputStream in the request body will be read, and then the two parameters will be parsed in a loop. When the first parameter is parsed, the inputStream will be closed, and the second parameter will be read again. When inputStream is read, it is empty.
In this case, all incoming parameters will be assigned to the first String object, and the second String will be an empty string after parsing.

3. The second type: one object parameter, one String parameter

For the case where the first parameter is an encapsulated object, the first object can be parsed, but the second parameter cannot be obtained.
In this case, no error will be reported, but there is no problem parsing the first object, and parsing the second String will get an empty string.

@POST
@Path("demo")
@Consumes({MediaType.JSON})
public Result function(MyParam myParam, String param);


Incoming parameters:
{"str":"helo","integer":2},"string":"test"

Parsed parameters:

param1: The object myParam can be parsed correctly, and its two properties can be assigned correctly.
param2: ""


4. The third type: a String parameter, an object parameter

If the positions of the two parameters are exchanged, all the incoming parameters will be parsed to the first String, and when the second object is parsed, because the obtained The data is empty, so an error will be reported. as follows:


@POST
@Path("demo")
public Result function(String param, MyParam myParam);


Incoming parameters:
{"str":"helo","integer":2},"string":"test"

Parsed parameters:

param1: "{"str":"helo","integer":2},"string":"test""
param2: will report an error


5. Solution

To solve the problem of passing in multiple parameters, there are several ideas:
1. Encapsulate the object, encapsulate the multiple parameters to be passed into an object and pass
it in 2. Embed variables in the access path and use the @PathVariable annotation , write "/demo/{1}/{2}" in the request path, and then replace the corresponding position in the request path with the parameters to be worn. This is only applicable to wrapper classes, such as String.
3. Change the content type of the request and use content-type: application/x-www-form-urlencoded. This form of form submission can pass in two parameters. Use the @FormParam annotation in combination

. 6. About using form Form incoming parameters The definition form of the

interface needs to be modified

@POST
@Path("demo")
@Consumes({MediaType.APPLICATION_FORM_URLENCODED})
public Result function(@FormParam(value="string1")String string1, @FormParam(value="string2")String string2);


When requesting, the header parameter needs to be modified

accept: application/json
content-type: application/x-www-form-urlencoded


请求Body中使用form形式

string1=wo&string2=kan


然后就可以正确解析到两个参数的值
解析出来的参数:

string1: wo
string2: kan

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327058741&siteId=291194637