请求代理转发(二)

请求代理转发(二)

书接上回,在内部服务代理其他服务时, 这次使用 spring-cloud-gateway-mvc 模块

引入依赖

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-gateway-mvcartifactId>
    <version>4.0.7version>
dependency>

代理服务

@RestController
public class ProxyController {
    @Value("${remote.home:http://localhost:9999/}")
    private URI home;

    /**
     * 可代理 get, post , 文件 上传
     * @param proxy
     * @param request
     * @param body
     * @return
     * @throws Exception
     */
    @RequestMapping("/proxy/**")
    public ResponseEntity<?> proxyPath(ProxyExchange<?> proxy, HttpServletRequest request,
                                       @RequestBody(required = false) String strBody) throws Exception {
        String path = proxy.path("/proxy/");
        HttpMethod method = HttpMethod.resolve(request.getMethod());
        String url = home.toString() + path + "?" + request.getQueryString();
        if (method.equals(GET)) {
            return proxy.uri(url).get();
        } else if (method.equals(POST)) {
            if (request instanceof MultipartHttpServletRequest) {
                MultiValueMap<String, Object> body
                        = new LinkedMultiValueMap<>();
                MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
                for (Map.Entry<String, List<MultipartFile>> entry : multipartRequest.getMultiFileMap().entrySet()) {
                    for (MultipartFile multipartFile : entry.getValue()) {
                        body.add(entry.getKey(), new ByteArrayResource(multipartFile.getBytes()) {
                            @Override
                            public String getFilename() {
                                return multipartFile.getOriginalFilename();
                            }
                        });
                    }
                }
                return proxy.uri(url).body(body).post();
            }
            return proxy.uri(url).body(strBody).post();
        }
        throw new RuntimeException("not support method");
    }
}

目标服务

/**
     * get 请求
     * @param name
     * @return
     */
@GetMapping("/input")
public String input(@RequestParam(required = false) String name) {
    System.out.println(Thread.currentThread().getName() + " request: " + name);
    return "success";
}

@Autowired
private ObjectMapper objectMapper;


/**
     * post json ect..
     * @param query
     * @return
     * @throws JsonProcessingException
     */
@PostMapping("/body")
public String body(@RequestBody DynamicCodeDto query) throws JsonProcessingException {
    System.out.println(Thread.currentThread().getName() + " request: " + objectMapper.writeValueAsString(query));
    return "success";
}

/**
     * post 表单
     * @param query
     * @return
     * @throws JsonProcessingException
     */
@PostMapping("/form")
public String body2(DynamicCodeDto query) throws JsonProcessingException {
    System.out.println(Thread.currentThread().getName() + " request: " + objectMapper.writeValueAsString(query));
    return "success";
}

/**
     * 上传文件
     * @param file
     * @param request
     * @return
     * @throws IOException
     */
@PostMapping("/formdata")
public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
    System.out.println(file.getSize());
    String s = getClass().getResource("/").getPath()  + file.getOriginalFilename();
    System.out.println(s);
    file.transferTo(new File(s));
    System.out.println(Thread.currentThread().getName() + " request: " + file.getName());
    return "success";
}

测试:

http-nio-9999-exec-5 request: test
http-nio-9999-exec-4 request: {"className":"1","code":"1"}
http-nio-9999-exec-6 request: {"className":"tsdd","code":"s"}
725445
/boot-mvc/target/classes/a.jpg

good luck!

你可能感兴趣的:(spring,boot,spring,boot)