Use Restful style used in the post less than encounter the front end of the back-end data transfer

 Problem Description: Using the postman request when the data front end, has been receiving less than to find a lot of information in the back-end, turned out to be because of a annotation.

The reasons @ RequestParam @ ReqeustBody settings: back-end reception setting

  Because ReqeustParam annotation spring receives parameters from requestHeader, i.e. the request header, i.e. the url format xxx? Username = 18 & password = 123, and ReqeustBody annotation parameters received comes from requestBody, i.e. the request body .

 

I am using a front-end Content-Type is default

application/json;charset=UTF-8

The server is

'Content-Type': 'application/x-www-form-urlencoded'

 

 

Some common set of back-end as follows:

One:

  If the request to get background notes should receive parameters RequestParam, if the request for the post, the background notes is to receive parameters RequestBody. Attach two examples, the following theme:

      get request

@RequestMapping (value = "/ anaData" )
     public String selectOrderInfo (@RequestParam ( "Method") String Method, @RequestParam the Map <String, Object> the params) { 
        log.debug (TextUtils.format ( "/ *** Data Analysis module, the general method of statistical inquiries 0} ** {/ " , method));
         return analysisClientService.selectOrderInfo (method, the params); 
    }

post request

 @PostMapping(value = "/center")
    public String requestApi(@RequestBody Map<String, Object> params){
        loger.debug(TextUtils.format("调用开始------"));
        return apiCenterClientService.requestApi(params);
    }

If a parameter has HttpServletRequest can be injected in the following manner

  Controller HttpServletRequest injected in the form as follows:

@RestController
public class AutowiredRequestController {
 
    @Autowired
    private HttpServletRequest request;
}

No thread-safety issues! Controller layer injected HttpServletReuqest implementation class to JDK dynamic proxy class generated by a proxy, obtain the value from the time the Request is a value obtained from the object in a ThreadLocal.

 

 

@RequestParam
for processing Content-Type: is application / x-www-form- urlencoded encoded content. Presented in a way to get or post. (The Http protocol, if not specified Content-Type, the default parameter passing is application / x-www-form- urlencoded type)

RequestParam essence the Key-Value parameter Request.getParameter () using the transformation mechanism Spring Map of ConversionService configuration, transforming into a parameter receiving object or field.

get value query String embodiment, the body and the post value of the data of the embodiment will be received and transformed into Request.getParameter Servlet () parameter set, you can get to @RequestParam

@RequestBody
processing HttpEntity data transmission over, typically used to process non-Content-Type: application data / x-www-form-urlencoded encoding format.

GET request, because there is no HttpEntity, so @RequestBody does not apply.
POST request, parameter passing HttpEntity, must declare the data type in the request header Content-Type, SpringMVC to parse the data by using HttpEntity HttpMessageConverters HandlerAdapter configuration, and then bound to the appropriate bean.

@RequestBody for post request, get request can not be used

This involves the use @RequestBody receive different objects
1. Create a new entity, the two entity are inside. This is the simplest, but not enough "elegant."
2. Map <String, Object> accepted request body, into their respective deserialize the entity.
3. The method is similar to, but more generic, to realize their HandlerMethodArgumentResolver.

 

When the current station interface uses GET or POST data submission, data coding format designated by the ContentType request header. Divided into the following situations:
1. file application / X-WWW-form-urlencoded, case data @ RequestParam, @ ModelAttribute can handle, @ RequestBody can also be treated.
2. multipart / form-data, @ RequestBody can not process the data in this format. (When there are file upload form form, must be specified enctype attribute value multipart / form-data, transport means in the form of a binary stream file.)
Data format 3. application / json, application / xml, etc., must be used! RequestBody to deal with.

 

Here stuck, the card can be uncomfortable, leaving this, give yourself a long point memory

Guess you like

Origin www.cnblogs.com/qingmuchuanqi48/p/11361320.html