从http请求中获取参数

springmvc, servlet 和 http 日后认真研究, 先记录下常用获取方式


post请求有请求体, get请求只有请求行和请求头,没有请求体

1.  直接从HttpServletRequest获取信息, 多在springmvc的拦截器中获取请求头中的信息, 用于权限校验

public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
    
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();

        String token = request.getHeader("token");
        String uid = request.getHeader("uid");


2.  get请求的参数在请求行中, post请求参数在请求体中, 二者普通类型参数的获取都可以通过springmvc的标注@RequestParam直接获得

public ResponseMap searchPage(@RequestParam(defaultValue = "0", required = false) int page, @RequestParam(defaultValue = "20", required = false) int count,
 @ApiParam(value = "搜索词", required = true) @RequestParam String key) {
        valid(key, count, page);
        return ResponseMap.success(integrateSearchService.search(key, count, page));
    }


3. post请求, 直接通过@RequestBody获取实体bean参数映射, 详见 http://blog.csdn.net/asdfsadfasdfsa/article/details/79108577

此处要设置contentType,contentType:"application/json,明确的告诉服务器发送的内容是json,而默认的contentType是application/x-www-form-urlencoded; charset=UTF-8

即获取实体bean参数提交的信息和接收的信息必须是application/json

@RequestMapping(value = "add", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
    
public ResponseMap add(@RequestBody ShopCartModel baseShopCart) {
        shopCartService.add(UserContext.getUserId(), baseShopCart);
        return getShopCartResponse();
    }



4. post请求上传文件, 通过@RequestParam获取文件流

 @ApiResponses({@ApiResponse(code = 200, message = "success", response = VoiceSearchResult.class)})
    public Object itemsPageByVoice(@RequestParam(defaultValue = "0", required = false) int page, @RequestParam(defaultValue = "20", required = false) int count,
 @RequestParam("voice") MultipartFile voice) {
        if (voice == null) {
            throw new ParamsError("缺少语音数据流");
        }
        String recognize;
        try {
            recognize = audioRecognizeService.recognize(voice.getBytes());
        } catch (IOException e) {
            throw new ParamsError("语音流获取失败");
        }
        Page<ItemSpecifications> page1 = integrateSearchService.search(recognize, count, page);
        List<ItemSpecifications> itemSpecifications = page1.getObjects();
        VoiceSearchResult result = new VoiceSearchResult(recognize, itemSpecifications);
        result.setHasNext(page1.isHasNext());
        return ResponseMap.success(result);
    }

5. 获取post请求的请求体中参数

 @RequestMapping(value = "alipay/payCallback", method = RequestMethod.POST)
    public String alipayCallback(HttpServletRequest request) {
        return alipayAsyncNotifyService.payCallBack(request);
    }


public String payCallBack(HttpServletRequest request) {
        String result = FAIL;
        String content;
        try {
            InputStream inStream = request.getInputStream();
            content = StringUtil.newStringUtf8(IOUtil.toByteArray(inStream));
.....
}

request.getInputStream(); 

request.getReader();

和request.getParameter("key");

这三个函数中任何一个函数执行一次后(可正常读取body数据),之后再执行就无效了




posted @ 2018-01-19 17:22  車輪の唄  阅读(40)  评论(0编辑  收藏  举报  来源