记录:392
场景:在微服务中获取http请求头信息方式,可以在代码中注入HttpServletRequest;可以使用Spring的RequestContextHolder,可以使用@RequestHeader注解。
版本:JDK 1.8,SpringBoot 2.6.3,springCloud 2021.0.1
1.使用HttpServletRequest获取请求头信息
1.1在微服务的Controller层获取请求头
本质就是使用注解@Autowired注入HttpServletRequest接口的实现对象。
@RestController
@RequestMapping("/hub/example/city")
@RefreshScope
@Slf4j
public class CityController {
// 注入HttpServletRequest
@Autowired
private HttpServletRequest request;
@PostMapping("/queryCityByCityId")
public ResultObj<CityDTO> queryCityByCityId(String cityId) {
// 从请求头取信息
log.info("cityCode = " + request.getHeader("cityCode"));
return ResultObj.data(200, new CityDTO(), "执行成功");
}
}
1.2在微服务的其它类获取请求头
在微服务Service、Dao、或者其它类中,只要有@RestController、@Controller、@Service、@Component、@Repository等注解的类中,都可以注入HttpServletRequest方式获取请求头信息。
2.使用RequestContextHolder获取请求头信息
2.1关于RequestContextHolder
org.springframework.web.context.request.RequestContextHolder,是Spring获取请求上下文工具类。此类提供的静态方法可以获取请求头信息。
2.2使用RequestContextHolder
RequestAttributes reqAttributes = RequestContextHolder.currentRequestAttributes();
HttpServletRequest request = ((ServletRequestAttributes) reqAttributes).getRequest();
String cityCode = request.getHeader("cityCode");
2.2使用场景
在任何Spring加载的类中均可以使用此类。
3.使用RequestContextHolder获取请求头信息
3.1关于@RequestHeader
org.springframework.web.bind.annotation.RequestHeader,spring注解。
3.2使用@RequestHeader
RestController
@RequestMapping("/hub/example/city")
@RefreshScope
@Slf4j
public class CityController {
@PostMapping("/queryCityByCityId")
public ResultObj<CityDTO> queryCityByCityId(@RequestParam("cityId") String cityId, @RequestHeader String cityNo) {
log.info("cityNo = " + cityNo);
return ResultObj.data(200, new Date(), "执行成功");
}
}
3.3使用@RequestHeader时,OpenFeign接口需适当调整
使用@RequestHeader注解看上去很方便,一个注解解决。
但是,使用@RequestHeader时,OpenFeign接口需适当调整。
比如:
@FeignClient(contextId = "cityFeignService",
value = "hub-example-301-nacos",
fallbackFactory = CityFeignServiceFallbackFactory.class,
configuration = {FeignConfiguration.class})
public interface CityFeignService {
@PostMapping("/hub-301-nacos/hub/example/city/queryCityByCityId")
ResultObj<CityDTO> queryCityByCityId(@RequestParam("cityId")String cityId);
}
4.关于Spring的HttpHeaders请求头对象
4.1HttpHeaders定义的属性
全称:org.springframework.http.HttpHeaders。
public static final String ACCEPT = "Accept";
public static final String ACCEPT_CHARSET = "Accept-Charset";
public static final String ACCEPT_ENCODING = "Accept-Encoding";
public static final String ACCEPT_LANGUAGE = "Accept-Language";
public static final String ACCEPT_PATCH = "Accept-Patch";
public static final String ACCEPT_RANGES = "Accept-Ranges";
public static final String ACCESS_CONTROL_ALLOW_CREDENTIALS = "Access-Control-Allow-Credentials";
public static final String ACCESS_CONTROL_ALLOW_HEADERS = "Access-Control-Allow-Headers";
public static final String ACCESS_CONTROL_ALLOW_METHODS = "Access-Control-Allow-Methods";
public static final String ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin";
public static final String ACCESS_CONTROL_EXPOSE_HEADERS = "Access-Control-Expose-Headers";
public static final String ACCESS_CONTROL_MAX_AGE = "Access-Control-Max-Age";
public static final String ACCESS_CONTROL_REQUEST_HEADERS = "Access-Control-Request-Headers";
public static final String ACCESS_CONTROL_REQUEST_METHOD = "Access-Control-Request-Method";
public static final String AGE = "Age";
public static final String ALLOW = "Allow";
public static final String AUTHORIZATION = "Authorization";
public static final String CACHE_CONTROL = "Cache-Control";
public static final String CONNECTION = "Connection";
public static final String CONTENT_ENCODING = "Content-Encoding";
public static final String CONTENT_DISPOSITION = "Content-Disposition";
public static final String CONTENT_LANGUAGE = "Content-Language";
public static final String CONTENT_LENGTH = "Content-Length";
public static final String CONTENT_LOCATION = "Content-Location";
public static final String CONTENT_RANGE = "Content-Range";
public static final String CONTENT_TYPE = "Content-Type";
public static final String COOKIE = "Cookie";
public static final String DATE = "Date";
public static final String ETAG = "ETag";
public static final String EXPECT = "Expect";
public static final String EXPIRES = "Expires";
public static final String FROM = "From";
public static final String HOST = "Host";
public static final String IF_MATCH = "If-Match";
public static final String IF_MODIFIED_SINCE = "If-Modified-Since";
public static final String IF_NONE_MATCH = "If-None-Match";
public static final String IF_RANGE = "If-Range";
public static final String IF_UNMODIFIED_SINCE = "If-Unmodified-Since";
public static final String LAST_MODIFIED = "Last-Modified";
public static final String LINK = "Link";
public static final String LOCATION = "Location";
public static final String MAX_FORWARDS = "Max-Forwards";
public static final String ORIGIN = "Origin";
public static final String PRAGMA = "Pragma";
public static final String PROXY_AUTHENTICATE = "Proxy-Authenticate";
public static final String PROXY_AUTHORIZATION = "Proxy-Authorization";
public static final String RANGE = "Range";
public static final String REFERER = "Referer";
public static final String RETRY_AFTER = "Retry-After";
public static final String SERVER = "Server";
public static final String SET_COOKIE = "Set-Cookie";
public static final String SET_COOKIE2 = "Set-Cookie2";
public static final String TE = "TE";
public static final String TRAILER = "Trailer";
public static final String TRANSFER_ENCODING = "Transfer-Encoding";
public static final String UPGRADE = "Upgrade";
public static final String USER_AGENT = "User-Agent";
public static final String VARY = "Vary";
public static final String VIA = "Via";
public static final String WARNING = "Warning";
public static final String WWW_AUTHENTICATE = "WWW-Authenticate";
public static final HttpHeaders EMPTY = new ReadOnlyHttpHeaders(new LinkedMultiValueMap());
private static final Pattern ETAG_HEADER_VALUE_PATTERN = Pattern.compile("\\*|\\s*((W\\/)?(\"[^\"]*\"))\\s*,?");
private static final DecimalFormatSymbols DECIMAL_FORMAT_SYMBOLS;
private static final ZoneId GMT;
private static final DateTimeFormatter DATE_FORMATTER;
private static final DateTimeFormatter[] DATE_PARSERS;
final MultiValueMap<String, String> headers;
4.2HttpMethod定义的enum属性
全称:org.springframework.http.HttpMethod。
public enum HttpMethod {
GET,
HEAD,
POST,
PUT,
PATCH,
DELETE,
OPTIONS,
TRACE;
}
以上,感谢。文章来源:https://www.toymoban.com/news/detail-686112.html
2023年3月24日文章来源地址https://www.toymoban.com/news/detail-686112.html
到了这里,关于使用HttpServletRequest或RequestContextHolder或@RequestHeader获取http请求头信息的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!