java web 网关转发接口实现

2020-09-11  本文已影响0人  舒尔诚

/**
* 网关不限制requstbody
* @param request
* @return
/
@ApiOperation(value = "gate", notes = "创建人 sjs 20200907")
@RequestMapping(value = "/gate", method = RequestMethod.POST)
public ModelAndView gate(/
@RequestBody @Valid FindParkFeeInfoReqTO formTo /HttpServletRequest request) {
/

FindParkFeeInfoRespTO findParkFeeInfoRespTO= apiOpenService.findParkFeeInfo(formTo);
System.out.println(Thread.currentThread().getId());
*/
//读取body内容
System.out.println("body:"+readBodyAsChars( request));

    return new ModelAndView("forward:/apiOpen/test" );//业务接口将自动获取header和body,因为共用一个request
}

/**
 * 目标接口
 * @param invokeSource
 * @param formTo
 * @return
 */
@ApiOperation(value = "test", notes = "创建人 sjs  20200907")
@RequestMapping(value = "/test", method = RequestMethod.POST)
//@ResponseBody
public CoreResponse<FindParkFeeInfoRespTO> test(@RequestHeader("invoke_source") String invokeSource,
        @RequestBody @Valid FindParkFeeInfoReqTO formTo) {
    FindParkFeeInfoRespTO r=new FindParkFeeInfoRespTO();
    r.setStoreId(999999);
    System.out.println(Thread.currentThread().getId());
    ModelAndView dd = new ModelAndView();
    return CoreResponse.success(r);
}

/**
 * 解析request的body内容
 * @param request
 * @return
 */
public static String readBodyAsChars(HttpServletRequest request){
    InputStream is = null;
    StringBuilder sb = new StringBuilder();
    try
    {
        is = request.getInputStream();
        
        byte[] b = new byte[4096];
        for (int n; (n = is.read(b)) != -1;)
        {
            sb.append(new String(b, 0, n));
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if (null != is)
        {
            try
            {
                is.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    return sb.toString();
}
上一篇下一篇

猜你喜欢

热点阅读