Spring mvc passthrough, springmvc

Source: Internet
Author: User

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.