Jersey Exception : SEVERE: A message body reader for Java class

后端 未结 15 1866
说谎
说谎 2020-11-29 11:08

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



        
相关标签:
15条回答
  • 2020-11-29 11:28

    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.

    1. Get required dependencies by one of the following:

      1. check for the IntelliJ running command. Stackoverflow-link
      2. List dependencies from maven mvn dependency:tree
    2. Now, add those problematic jersey dependencies explicitly.

    0 讨论(0)
  • 2020-11-29 11:30

    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.

    0 讨论(0)
  • 2020-11-29 11:30

    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;
    }
    
    0 讨论(0)
  • 2020-11-29 11:36

    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.

    0 讨论(0)
  • 2020-11-29 11:37

    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

    0 讨论(0)
  • 2020-11-29 11:38

    Just check if you are running different instances in eclipse. I quit all my other sessions, clean build fixed the problem

    0 讨论(0)
提交回复
热议问题