Spring mvc passthrough, springmvc
With the increase of the company's scale and projects, there will be a need for transparent transmission, and this layer of transparent transmission is used for permission control, gray release, and traffic statistics.
Notes for implementing passthrough:
1. Spring MVC implements url wildcard configuration. the backend service URLs are various and cannot solve this problem according to the length you have imagined.
@RequestMapping(value = "/{serviceName}/{methodName}/**/", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"}) public @ResponseBody Object getHttp(@PathVariable(value = "serviceName") String serviceName, @PathVariable(value = "methodName") String methodName, HttpServletRequest request, HttpServletResponse response) throws ServiceException { try { return sendGetHttp(serviceName, methodName, request, response); } catch (ServiceException e) { LOGGER.error("getHttp_service", e); response.setStatus(520); return ResponseEntity.fail(e.getErrorCode(), e.getErrorMessage()); } catch (SystemException e) { LOGGER.error("getHttp_system", e); response.setStatus(520); return ResponseEntity.error(e.getErrorMessage()); } catch (Exception e) { LOGGER.error("getHttp_exception", e); response.setStatus(520); return ResponseEntity.error(e.getMessage()); } }
2. body stream parsing, POST/PUT/PATCH, is generally a request containing the body, but as a transparent transport layer, there is a request that does not contain the body, therefore, transparent transmission requests are not suitable for adding @ RequestBody. At this time, we can parse the body information through HttpServletRequest.
// Parse the body private String resolveRequestToBody (HttpServletRequest request) throws IOException {int length = request. getContentLength (); String requestStr = null; if (length! = 0) {InputStream inputStream = request. getInputStream (); byte B [] = new byte [length]; inputStream. read (B); inputStream. close (); requestStr = new String (B); LOGGER.info ("requestLength:" + length + ", requestType:" + request. getContentType () + ", body:" + requestStr);} return requestStr ;}
Appendix:
Wildcard details to recommend a blog: http://www.codeweblog.com/springmvc%E7%AC%94%E8% AE %B0%E7%B3%BB%E5%88%97-5-requestmapping%E8%AF%B7%E6%B1%82value%E7%9A%84%E9%80%9A%E9%85%8D%E7%AC%A6%E8%AF%A6%E8%A7%A3/