Привет всем, я просто хочу знать, как мы можем установить свойство объекта ClientRequestContext в динамическое значение при использовании ClientRequestFilter в веб-сервисе jax-rs. просто так
Предположим, у меня есть объект
MyObject obj=new MyObject();
obj.nmae="simon";
obj.vendor=209;
Теперь я хочу вызвать веб-сервис, для которого я создаю клиент-джерси, и для этого клиента я создал фильтр. Теперь я хочу узнать, как передать свой объект в clientFilter, чтобы я мог установить свойство ClientRequestContext в myObject.
@Provider
public class RequestClientFilter implements ClientRequestFilter {
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
// how to set this to a variable value passed from a function in my code
requestContext.setProperty("test", "test client request filter");
//something along these lines
requestContext.setProperty("myobj",obj);
}
}
это то, что происходит
MyObject obj=new MyObject();
obj.nmae="simon";
obj.vendor=209;
ClientConfig config = new ClientConfig();
config.register(EntityLoggingFilter.class);
config.register(GsonMessageBodyHandler.class);
config.register(ClFilter.class);
// config.property(ClientProperties.CONNECT_TIMEOUT, 1000);
config.property(ClientProperties.READ_TIMEOUT, 10000);
try {
Client client = ClientBuilder.newClient(config);
WebTarget webTarget = client.target("http://localhost:8081/ServicB").path("Register")
.path("Service");
System.out.println(webTarget.getUri());
Invocation.Builder invocationBuilder = webTarget.request().accept(MediaType.APPLICATION_JSON);
Response response = invocationBuilder.post(Entity.json("anything"));
System.out.println(response.getStatus());
String bresponse = response.readEntity(String.class);
if (bresponse == null) {
System.out.println("null response");
}
System.out.println(bresponse);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}