MessageBodyWriter not found for media type=application/json

后端 未结 10 1517
庸人自扰
庸人自扰 2020-11-30 02:13

I am facing issues while consuming JAX-RS services as JSON.

Below I have added my code.

This is my service class:

//Sets the path to base URL         


        
相关标签:
10条回答
  • 2020-11-30 02:40

    Uncommenting the below code helped

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>
    

    which was present in pom.xml in my maven based project resolved this error for me.

    0 讨论(0)
  • 2020-11-30 02:40

    Below should be in your pom.xml above other jersy/jackson dependencies. In my case it as below jersy-client dep-cy and i got MessageBodyWriter not found for media type=application/json.

    <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-json-jackson</artifactId>
      <version>2.25</version>
    </dependency>
    
    0 讨论(0)
  • 2020-11-30 02:40

    In my experience this error is pretty common, for some reason jersey sometimes has problems parsing custom java types. Usually all you have to do is make sure that you respect the following 3 conditions:

    1. you have jersey-media-json-jackson in you pom.xml if using maven or added to your build path;
    2. you have an empty constructor in the data type you are trying to de-/serialize;
    3. you have the relevant annotation at the class and field level for your custom data type (xmlelement and/or jsonproperty);

    However, I have ran into cases where this just was not enough. Then you can always wrap you custom data type in a GenericEntity and pass it as such to your ResponseBuilder:

    GenericEntity<CustomDataType> entity = new GenericEntity<CustomDataType>(myObj) {};
    return Response.status(httpCode).entity(entity).build();
    

    This way you are trying to help jersey to find the proper/relevant serialization provider for you object. Well, sometimes this also is not enough. In my case I was trying to produce a text/plain from a custom data type. Theoretically jersey should have used the StringMessageProvider, but for some reason that I did not manage to discover it was giving me this error:

    org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/plain
    

    So what solved the problem for me was to do my own serialization with jackson's writeValueAsString(). I'm not proud of it but at the end of the day I can deliver an acceptable solution.

    0 讨论(0)
  • 2020-11-30 02:43

    I think may be you should try to convert the data to json format. It can be done by using gson lib. Try something like below.

    I don't know it is a best solutions or not but you could do it this way.It worked for me

    example : `

    @GET
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public Response info(@HeaderParam("Accept") String accept) {
        Info information = new Info();
        information.setInfo("Calc Application Info");
        System.out.println(accept);
        if (!accept.equals(MediaType.APPLICATION_JSON)) {
            return Response.status(Status.ACCEPTED).entity(information).build();
        } else {
            Gson jsonConverter = new GsonBuilder().create();
            return Response.status(Status.ACCEPTED).entity(jsonConverter.toJson(information)).build();
        }
    }
    
    0 讨论(0)
  • 2020-11-30 02:44

    I was able to fix it by install jersey-media-json-jackson

    Add the dependency to pom.xml

    <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-json-jackson</artifactId>
      <scope>runtime</scope>
    </dependency>
    
    0 讨论(0)
  • 2020-11-30 02:45

    To overcome this issue try the following. Worked for me.

    Add following dependency in the pom.xml

    <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-json-jackson</artifactId>
      <version>2.25</version>
    </dependency>
    

    And make sure Model class contains no arg constructor

     public Student()
        {
        }
    
    0 讨论(0)
提交回复
热议问题