I have a Jersey based Rest WS which outputs JSON. I am implementing a Jersey Client to invoke the WS and consume the JSON response. The client code I have is below
Q) Code was working fine in Intellj but failing in command line.
Sol) Add dependencies of jersey as a direct dependency rather than a transient one.
Reasoning: Since, it was working fine with IntelliJ, dependencies are correctly configured.
Get required dependencies by one of the following:
mvn dependency:tree
Now, add those problematic jersey dependencies explicitly.
If you are building an uberjar or "shaded jar", make sure your meta inf service files are merged. (This bit me multiple times on a dropwizard project.)
If you are using the gradle shadowJar plugin, you want to call mergeServiceFiles()
in your shadowJar
target: https://github.com/johnrengelman/shadow#merging-service-files
Not sure what the analogous commands are for maven or other build systems.
You need to implement your own MessageBodyReader
and MessageBodyWriter
for your class Lorg.shoppingsite.model.entity.jpa.User
.
package javax.ws.rs.ext;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
public interface MessageBodyReader<T extends Object> {
public boolean isReadable(Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType);
public T readFrom(Class<T> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, String> httpHeaders,
InputStream entityStream) throws IOException, WebApplicationException;
}
package javax.ws.rs.ext;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
public interface MessageBodyWriter<T extends Object> {
public boolean isWriteable(Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType);
public long getSize(T t,
Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType);
public void writeTo(T t,
Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream) throws IOException, WebApplicationException;
}
Try adding:
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>1.4</version>
</dependency>
Also this problem may occur if you're using HTTP GET
with message body so in this case adding jersey-json lib, @XmlRootElement
or modifying web.xml won't help. You should use URL QueryParam
or HTTP POST
.
Some may be confused why add jersey-json jar can't solve this problem. I found out that this jar has to newer than jersey-json-1.7.jar( 1.7.0 doesn't work, but 1.7.1 works fine.). hope this can help
Just check if you are running different instances in eclipse. I quit all my other sessions, clean build fixed the problem