postman use (a)

I. Overview

  For the test interface, you can use the client version of the postman, postman plug-ins can also be installed directly on the chrome browser. The client version of the postman

More powerful features, but for the interface needs to be logged in to post test, when the test need to set up a separate authentication parameters such as sessionid; for plug-in version of the postman,

After the test need only visit the website, you can set up a direct request parameters, it is unnecessary to set this parameter sessionid.

 

1. The data request body type:

(1)form-data(post)

  Either upload key-value pairs, you can also upload files (multipart / form-data). When the file is uploaded field, Content-Type would be the type of documentation;

content-disposition to illustrate some of the information fields; MultipartHttpServletRequest may be used in accordance with api springmvc received by "name"

Get different keys, may also receive a plurality of files MulTipartFile array;


(2)x-www-form-urlencoded(post)

   The data will be converted in the form of key-value pairs, such as, name = java & age = 23 . Using @RequestParam rear end to receive the parameters. @RequestParam used

Processing Content-Type: application for the content / x-www-form-urlencoded encoding, submission GET, POST, @RequestParam can get treated the way

QueryString value, the post processing may be described in the body data values, @ RequestParam underlying parameters are obtained by request.getParameter manner.

Request objects are passed Json distal backend @RequestParam (without the rear end of this or the default mode).

(3)raw(post)

   Can upload any format text, upload text, json, xml, html, etc.; @RequestBody accept a string json object, rather than Json objects ,

When requested often Json objects, the object will be able to become json string JSON.stringify (data) manner. String object Json distal pass the request to use the rear @RequestBody .

(4)binary(post)

  Can only upload binary data, typically used to upload the file, because there is no key, so only upload one file (equivalent to Content-Type: application / octet-stream);

 

2. Notes the difference of @RequestParam and @RequestBody

(1)@RequestParam

@RequestParam annotations from requestHeader received parameter, i.e. the request header. GET requests typically used, like other types of POST, DELETE request and the like may also be used.

Example of use:

Request url: 
HTTP: // xxx: 8080 / getByNameAndType name of the type = 1 = Faye &? 

@RequestParam There are three configuration parameters: 
required must indicate whether the default is to true , you must; 
defaultValue can set the default value of request parameters; 
value to receive url parameter name (corresponding to the key value);
 
@RequestMapper (value = "getByNameAndType", Method = RequestMethod.GET)    
 public List <the User> getByNameAndType (@RequestParam (value = "name", required = to true , defaultValue = "Yifei" ) String name, 
@RequestParam (value = "type", required = to true , defaultValue = ". 1" ) String type) {} ...... @RequestParam to process the Content

 -Type is application / x-www-form -urlencoded content encoding, Content- Type default for the property.
@RequestParam may also be used for other types of requests, such as: POST, DELETE and so on request. Such as inserting data into a single table, the wording Controller layer as shown below:
 
@RequestMapper (value = "Save", Method = RequestMethod.POST)    
 public  void Save (the User User) ......} {

(2) @RequestBody
annotated @RequestBody requestBody from the received parameters, i.e. the request body. Usually for processing non-Content-Type: application / x- www-form-urlencoded
data encoding format, such as: the type of application / json, application / xml data and the like. Typically for receiving POST, DELETE requests and other types of data, GET types may be applied.
On application / json type of data, you can use annotations @RequestBody body inside all json data to the back-end, back-end and then parse.

@RequestMapper(value="save", method=RequestMethod.POST)    
public void save(@RequestBody User user){......}

Since the process can be used to @RequestBody content Content-Type application / json coded, the postman, the body type is selected
row -> JSON (application / json ), this will also automatically changed in Headers Content-Type: application / json encoding format.
key attribute values correspond to the rear end of the body and the entity class inside json statement.
Note: $ .ajax front-end, it must be specified contentType: "application / json; charset = utf-8;", the default is application / x-www-form- urlencoded.

Second, the use

I used here is in the form of chrome plugin:

1. get

Back-end code:

@GetMapping("get")
public ResponseVO get(Integer id){
    return ResponseVO.success(roleService.getById(id));
}

 

2. post

(1) @RequestParam: the front end of the object needs to pass json

    When the rear end of the receiving array employed, may be provided for multiple key-value, eg when the front end of the test were:

    ids:1  ids:2 ids:3

@PostMapping("delete")
public ResponseVO delete(@RequestParam("ids") Integer[] ids){
    roleService.delete(ids);
    return ResponseVO.success();
}

 

(2) @RequestBody: json string received by the rear end

@PostMapping("edit")
public ResponseVO edit(@RequestBody RolePo rolePo){
    return ResponseVO.success(roleService.save(rolePo));
}

 

 

 

 

Reference documents:

https://blog.csdn.net/feiyst/article/details/88431621

Notes @RequestParam and @RequestBody usage scenarios

 

Guess you like

Origin www.cnblogs.com/shiyun32/p/10837901.html