1、
package com.yootk.consumer.interceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Slf4j // 添加日志注解
@Component // 配置之后自动拦截生效
public class MicroServiceHTTPInterceptor implements ClientHttpRequestInterceptor
{
@Override
public ClientHttpResponse intercept(HttpRequest request,byte[] body,ClientHttpRequestExecution execution)throws IOException
{
log.info("【HTTP请求拦截】服务主机:{}、REST路径:{}", request.getURI().getHost(), request.getURI().getPath());
// 此时可以将一些Token的数据保存在头信息之中,会随着每次的请求一起发送到服务端
request.getHeaders().set("token", "www.yootk.com"); // 随意添加的头信息
return execution.execute(request, body); // 发送请求
}
}
2、
package com.yootk.consumer.config;
import com.yootk.consumer.interceptor.MicroServiceHTTPInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import java.util.Collections;
@Configuration
public class RestTemplateConfig { // 实现RestTemplate的相关配置
@Autowired
private MicroServiceHTTPInterceptor interceptor; // 注入拦截器
@Bean // 向Spring容器之中进行Bean注册
public RestTemplate getRestTemplate() {
RestTemplate template = new RestTemplate();
template.setInterceptors((Collections.singletonList(this.interceptor) );
return template;
}
}
3、
package com.yootk.provider.action;
import com.yootk.common.dto.DeptDTO;
import com.yootk.service.IDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
@RestController
@RequestMapping("/provider/dept/*") // 微服务提供者父路径
@Slf4j // 使用一个注解
public class DeptAction {
@Autowired
private IDeptService deptService;
@GetMapping("get/{id}")
public Object get(@PathVariable("id") long id) {
this.printRequestHeaders("get");
return this.deptService.get(id);
}
@PostMapping("add")
public Object add(@RequestBody DeptDTO deptDTO) { // 后面会修改参数模式为JSON
this.printRequestHeaders("add");
return this.deptService.add(deptDTO);
}
@GetMapping("list")
public Object list() {
this.printRequestHeaders("list");
return this.deptService.list();
}
@GetMapping("split")
public Object split(int cp, int ls, String col, String kw) {
this.printRequestHeaders("split");
return this.deptService.split(cp, ls, col, kw);
}
private void printRequestHeaders(String restName) { // 实现所有请求头信息的输出
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
Enumeration<String> headerEnums = request.getHeaderNames();
while (headerEnums.hasMoreElements()) {
String headerName = headerEnums.nextElement();
log.info("【{}】头信息:{} = {}", restName, headerName, request.getHeader(headerName));
}
}
}
标签:HTTP,yootk,request,springframework,0207,import,org,拦截,public From: https://www.cnblogs.com/cnetsa/p/17007154.html