gpt4 book ai didi

java - 访问请求 header

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:13:52 27 4
gpt4 key购买 nike

JAX-RS中WriterInterceptor接口(interface)的实现如何访问请求头?

context.getHeaders(); //This line gives a set of response headers(not request headers) in the WriterInterceptor implementation.

完整代码如下:

public class GzipFilterWriterInterceptor implements WriterInterceptor {

private static final Logger LOG = LoggerFactory.getLogger(GzipFilterWriterInterceptor.class);

@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
MultivaluedMap<String,Object> headers = context.getHeaders();
headers.add("Content-Encoding", "gzip");


final OutputStream outputStream = context.getOutputStream();
context.setOutputStream(new GZIPOutputStream(outputStream));
context.proceed();
}

}

最佳答案

你可以只注入(inject) HttpHeaders .注入(inject)时它将是一个线程本地代理,因此它是线程安全的。

@Context
private HttpHeaders headers;

它有方法


更新(测试)

import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.WriterInterceptor;
import javax.ws.rs.ext.WriterInterceptorContext;
import java.io.IOException;
import java.util.logging.Logger;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
* Run like any other JUnit test. Only one required dependency:
*
* <dependency>
* <groupId>org.glassfish.jersey.test-framework.providers</groupId>
* <artifactId>jersey-test-framework-provider-inmemory</artifactId>
* <scope>test</scope>
* <version>${jersey.version}</version>
* </dependency>
*/
public class HeadersTest extends JerseyTest {

@Path("hello")
public static class HelloResource {
@GET
public String get() {
return "Hello";
}
}

@Override
public ResourceConfig configure() {
return new ResourceConfig(HelloResource.class)
.register(HeaderWriter.class)
.register(new LoggingFilter(Logger.getAnonymousLogger(), true));
}

@Provider
public static class HeaderWriter implements WriterInterceptor {
@Context
private HttpHeaders headers;

@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
context.proceed();

final String header = headers.getHeaderString("X-Request-Header");
context.getHeaders().add("X-Response-Header", header);
}
}

@Test
public void doit() {
final Response response = target("hello").request()
.header("X-Request-Header", "BooYah")
.get();
assertThat(response.getHeaderString("X-Response-Header"), is("BooYah"));
}
}

关于java - 访问请求 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40779426/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com