我以这种方式注入我的 cookie 参数(使用 javax.ws.rs.CookieParam)
@CookieParam("parameterCookie") 私有字符串参数Cookie;
我在使用 Resteasy 注入该参数时遇到问题
错误
将 @CookieParam 注入单例是非法的
那是一个 BaseResource ,我无法修改所有资源以在所有方法上接受该参数(这会花费很多)。我如何在不修改所有资源的情况下将 CookieParam 注入 Resteasy?
您可以通过注入来解决此问题HttpHeaders
:
import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.mock.MockDispatcherFactory;
import org.jboss.resteasy.mock.MockHttpRequest;
import org.jboss.resteasy.mock.MockHttpResponse;
import org.junit.Before;
import org.junit.Test;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
public class CookieTest {
static final String COOKIE_NAME = "parameterCookie";
Dispatcher dispatcher;
@Before
public void setUp() throws Exception {
dispatcher = MockDispatcherFactory.createDispatcher();
dispatcher.getRegistry().addSingletonResource(new Resource());
}
@Test
public void name_StateUnderTest_ExpectedBehavior() throws Exception {
String cookieValue = String.valueOf(System.currentTimeMillis());
MockHttpResponse response = new MockHttpResponse();
MockHttpRequest request = MockHttpRequest.get("/")
.cookie(COOKIE_NAME, cookieValue);
dispatcher.invoke(request, response);
assertThat(response.getContentAsString(), is(COOKIE_NAME + "=" + cookieValue));
}
@Path("/")
public static class Resource {
@Context HttpHeaders headers;
@GET @Path("/")
public String getCookie(){
return headers.getCookies().get(COOKIE_NAME).toString();
}
}
}