HTTPS using Jersey Client

Construct your client as such HostnameVerifier hostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier(); ClientConfig config = new DefaultClientConfig(); SSLContext ctx = SSLContext.getInstance(“SSL”); ctx.init(null, myTrustManager, null); config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(hostnameVerifier, ctx)); Client client = Client.create(config); Ripped from this blog post with more details: http://blogs.oracle.com/enterprisetechtips/entry/consuming_restful_web_services_with For information on setting up your certs, see this nicely answered SO question: Using HTTPS with REST … Read more

Handling custom error response in JAX-RS 2.0 client library

I believe you want to do something like this: Response response = builder.get( Response.class ); if ( response.getStatusCode() != Response.Status.OK.getStatusCode() ) { System.out.println( response.getStatusType() ); return null; } return response.readEntity( MyEntity.class ); Another thing you can try (since I don’t know where this API puts stuff — i.e. in the header or entity or what) … Read more

How does one intercept a request during the Jersey lifecycle?

I’ve found the answer. First, create a class that implements ContainerRequestFilter. The interface specifies the following method, in which the filtering takes place. The ContainerRequest object contains information about the current request. public ContainerRequest filter(ContainerRequest req); After that, include the following XML in the servlet configuration in web.xml <init-param> <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name> <param-value>path.to.filtering.class</param-value> </init-param> Sources: http://jersey.576304.n2.nabble.com/ContainerRequestFilter-and-Resources-td4419975.html http://markmail.org/message/p7yxygz4wpakqno5

Jersey Client API – authentication

At first I got this working as documented in the Jersey User guide Authenticator.setDefault (authinstance); However I did not like this as it relied on setting a global authenticator. After some research I discovered that Jersey has a HTTPBasicAuthFilter which is even easier to use. Client c = Client.create(); c.addFilter(new HTTPBasicAuthFilter(user, password)); See: https://jersey.github.io/nonav/apidocs/1.10/jersey/com/sun/jersey/api/client/filter/HTTPBasicAuthFilter.html https://jersey.github.io/nonav/apidocs/1.10/jersey/com/sun/jersey/api/client/filter/Filterable.html#addFilter(com.sun.jersey.api.client.filter.ClientFilter)

Change Dropwizard default ports

You can update the ports in your yaml configuration file: http: port: 9000 adminPort: 9001 See http://www.dropwizard.io/0.9.2/docs/manual/configuration.html#http for more information. EDIT If you’ve migrated to Dropwizard 0.7.x, 0.8.x, 0.9.x you can use the following: server: applicationConnectors: – type: http port: 9000 adminConnectors: – type: http port: 9001

How do you map multiple query parameters to the fields of a bean on Jersey GET request?

In Jersey 2.0, you’ll want to use BeanParam to seamlessly provide what you’re looking for in the normal Jersey style. From the above linked doc page, you can use BeanParam to do something like: @GET @Path(“find”) @Produces(MediaType.APPLICATION_XML) public FindResponse find(@BeanParam ParameterBean paramBean) { String prop1 = paramBean.prop1; String prop2 = paramBean.prop2; String prop3 = paramBean.prop3; … Read more