Why would “java.lang.IllegalStateException: The resource configuration is not modifiable in this context.” appear deploying Jersey app?

One possible cause is that you have two or more applicable mappings for that URL call. For example: @Path(“/{myParam}”) And somewhere else: @Path(“/{differentParam}”) Now Jersey have no way of telling what method is actually supposed to be called and gives this error.

Jersey 2 injection source for multipart formdata

You need to enable MultiPart feature on your application. Enabling this feature injects necessary message body readers, writers to your Jersey 2 application. Here is how you register them: On the server-side (http-server): final ResourceConfig resourceConfig = new ResourceConfig(MultiPartResource.class); resourceConfig.register(MultiPartFeature.class); On the server-side (servlet deployment): import org.glassfish.jersey.filter.LoggingFilter; import org.glassfish.jersey.media.multipart.MultiPartFeature; import javax.ws.rs.core.Application; import java.util.HashSet; import java.util.Set; … Read more

How to deploy a JAX-RS application?

There are a number of options for deploying into a Java EE 6 container (more specifically a Servlet 3.0 implementation): The simplest is: <?xml version=”1.0″ encoding=”UTF-8″?> <web-app xmlns=”http://java.sun.com/xml/ns/j2ee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_3_0.xsd” version=”3.0″> <servlet> <servlet-name>javax.ws.rs.core.Application</servlet-name> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>javax.ws.rs.core.Application</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app> Then all the @Path and @Provider classes found in your web application will be … Read more

Jersey + Jackson JSON date format serialization – how to change the format or use custom JacksonJsonProvider

I managed to do it in Resteasy “the JAX-RS way”, so it should work on every compliant implementation like Jersey (recently successfully tested on JEE7 server Wildfly 8, it just required a few changes to the Jackson part because they changed a few APIs). You must define a ContextResolver (check that Produces contains the correct … Read more

A message body writer for Java type, class myPackage.B, and MIME media type, application/octet-stream, was not found

In your client code you are not specifying the content type of the data you are sending – so Jersey is not able to locate the right MessageBodyWritter to serialize the b1 object. Modify the last line of your main method as follows: ClientResponse response = resource.type(MediaType.APPLICATION_XML).put(ClientResponse.class, b1); And add @XmlRootElement annotation to class B … Read more

How to return a partial JSON response using Java?

If you use Jackson (a great JSON lib – kind of the standard for Java I believe), you may use the @View annotation to filter what you want in the resulting object. I understand that you want something dynamic so it’s a bit more complicated. You will find what you are looking for here: http://www.cowtowncoder.com/blog/archives/2011/02/entry_443.html … Read more

REST response code for invalid data

400 is the best choice in both cases. If you want to further clarify the error you can either change the Reason Phrase or include a body to explain the error. 412 – Precondition failed is used for conditional requests when using last-modified date and ETags. 403 – Forbidden is used when the server wishes … Read more

How can I grab all query parameters in Jersey JaxRS?

You can access a single param via @QueryParam(“name”) or all of the params via the context: @POST public Response postSomething(@QueryParam(“name”) String name, @Context UriInfo uriInfo, String content) { MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(); String nameParam = queryParams.getFirst(“name”); } The key is the @Context jax-rs annotation, which can be used to access: UriInfo, Request, HttpHeaders, SecurityContext, … Read more

tech