jersey-2.0

Using Jersey 2.0, how do you register a bindable instance per request?

倖福魔咒の 提交于 2019-11-30 00:09:44
...if the instance needs to be constructed manually, perhaps by a 3rd party factory class? Previously, (Jersey 1.x), you would do something like this: public class MyInjectableProvider extends PerRequestTypeInjectableProvider<Context, MyInjectable> { public MyInjectableProvider() { super(MyInjectable.class); } @Override public Injectable<MyInjectable> getInjectable(ComponentContext ic, Context context) { MyInjectable myInjectableInstance = //... return new Injectable<MyInjectable>() { @Override public MyInjectable getValue() { return myInjectableInstance; } }; } } The anonymous local class is

Change ContentType or CharacterEncoding in Java Filter ONLY IF ContentType === JSON

ε祈祈猫儿з 提交于 2019-11-29 21:45:10
问题 I'm trying to ensure that all JSON responses from a Jersey based java application have a UTF-8 character encoding parameter appended to their ContentType header. So if it's a JSON response, I would like the response header for the Content-Type to be Content-Type: application/json;charset=UTF-8 EDIT: I know I can do this on a case by case basis, but I'd like to do it globally, so it affects all content responses that have a content type of "application/json". If I just try and set the

Serialize javax.ws.rs Entity to json

给你一囗甜甜゛ 提交于 2019-11-29 19:53:51
问题 I want to serizalize to Json with org.glassfish.jersey implementation Map<String, String> entity = Maps.newHashMap(); entity.put("foo", "bar"); Response response = Response.status(Response.Status.OK) .entity(entity) .type(MediaType.APPLICATION_JSON).build(); System.out.println(response.getEntity()); This map serialize to non standard { foo: "bar" } . I want to test this behaviour in unit test. 回答1: You can't test like this. What you are doing here Response response = Response.status(Response

REST JAX-RS javax.ws.rs.ProcessingException:

对着背影说爱祢 提交于 2019-11-29 18:23:33
问题 I am getting below exception whenever my REST client code makes a call to the REST service using below code: Code: public void putWatcher(Watcher watcher) { System.out.println("In REST Client putWatcher.***********"); target = target.path(RESOURCE_WATCHERS).path(watcher.getWatcheruri()); System.out.println(target.getUri()); Invocation.Builder builder = target.request(); builder.put(Entity.json(watcher)); // Response response = target.request().put(Entity.json(watcher)); System.out.println(

How can I get the stack trace when 500 server error happens in Jersey?

◇◆丶佛笑我妖孽 提交于 2019-11-29 17:45:54
When in jersey server I have server 500 error: In server response don´t have stack trace or some info. In Eclipse console don´t have exception stack trace I try to catch exception in server and print trace in console, but nothing happens How can I get the stack trace when 500 server error happens? Most of the time, a generic ExceptionMapper will do the trick. @Provider public class DebugMapper implements ExceptionMapper<Throwable> { @Override public Response toResponse(Throwable t) { t.printStackTrace() return Response.serverError() .entity(t.getMessage()) .build(); } } Then just register it

Jersey version issue: MessageBodyReader not found for media type=application/xml

寵の児 提交于 2019-11-29 17:14:43
问题 While writing a simple Jersey client that was consuming XML data, I came across this exception "MessageBodyReader not found for media type=application/xml". All of my settings, including the jersey-client as maven dependencies was just fine. The version that I was using was 2.17. Once I degraded the version to 2.15 it started working fine. Can anyone explain what dependencies that needs to be included for version 2.17 to work. Maven Dependency (works on 2.15 and lower) <dependency> <groupId

HK2 IterableProvider named method not finding Implementation

别来无恙 提交于 2019-11-29 16:58:09
I have a problem trying to inject a contract with two services bound to it. I'm using Jersey, and extending ResourceConfig to configure my app, where I'm binding two different implementations (classes FooImpl1 and FooImpl2 ) to a same contract (interface Foo ), ranking them differently. Each of these implementations is annotated with @Named and its name. In one of my controllers I want to have access to both implementations, so I inject an IterableProvider<Foo> fooProvider . If I do not specify anything, the implementation with the highest rank is injected always, which is what I want. The

calling flush() on Jersey StreamingOutput has no effect

穿精又带淫゛_ 提交于 2019-11-29 14:13:08
I am using a Jersey StreamingOutput that was working just fine until we upgraded to Jersey 2.16. Here's the thing. My StreamingOuput produces output very slowly in some circumstances. I do write data regularly, but I write it pretty slowly and just a little of it at a time. I call flush() on the OutputStream passed to StreamingOutput.write() every time I write any bytes, but the flush() appears to have no effect. Nothing is sent over the wire until 8K has been written to the OutputStream . Unfortunately, in some circumstances, by the time 8K has been written, the client has timed out. I

Do we still need JacksonFeature.class for Jersey 2.17 projects?

…衆ロ難τιáo~ 提交于 2019-11-29 13:58:08
I have been trying to know if JacksonFeature.class is still needed for Jersey 2.17. I can't see any difference between the outputs between codes that JacksonFeature.class is registered or not. Then, I forked a code from codingpedia codingpedia , removed JacksonFeature.class, upgraded to Spring 4.1.2 and jersey 2.17, updated the codes and the test still passed. So I created a very simple web service to test this again github link , having in mind to remove all the moving parts and still worked. So do we still need to register JacksonFeature? Yeah I don't know why that tutorial they are using

Unable to Mock Glassfish Jersey Client response object

落爺英雄遲暮 提交于 2019-11-29 13:46:53
I am having problems with creating a mock Response object to use with my unit tests. I am using org.glassfish.jersey.core.jersey-client version 2.3.1 to implement my RESTful client and mockito version 1.9.5 to help me with mock objects. Here is my test's code: @Test public void testGetAll() throws IOException { // Given String expectedResource = "expectedResource" final Response expectedRes = Response.ok(expectedResource, MediaType.APPLICATION_JSON).build(); String receivedResource; BDDMockito.given(this.client.getSimpleClient().getAllWithResponse()).willReturn(expectedRes); // When