简体   繁体   中英

jackson - MessageBodyWriter Not Found when returning a JSON POJO from a GET Api

I was trying to return a simple json response using jackson.

@GET
    @Produces(MediaType.APPLICATION_JSON)
    public FMSResponseInfo test(){
        System.out.println("Entered the function");
        FMSResponseInfo fmsResponseInfo = new FMSResponseInfo();
        List<SearchDetailsDTO> searchDetailsDtoList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            SearchDetailsDTO searchDetailsDto = new SearchDetailsDTO();
            searchDetailsDto.setBarcode("barcode" + i);
            searchDetailsDto.setDocNo("docNo" + i);
            searchDetailsDto.setDocType("docType" + i);
            searchDetailsDtoList.add(searchDetailsDto);
        }
        fmsResponseInfo.setStatus("200");
        fmsResponseInfo.setMessage("Success");
        fmsResponseInfo.setData(searchDetailsDtoList);
        System.out.println("Exiting the function");
        return fmsResponseInfo;
    }

This is the code. When the function tries to return the FMSResponseInfo which is POJO :

package com.fms.core;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "status", "message", "data" })
public class FMSResponseInfo {
    @JsonProperty("status")
    private String status;
    @JsonProperty("message")
    private String message;
    @JsonProperty("data")
    private Object data;
    //Getters and Setters
}

This is the pojo which will be sent back.

But as soons as the function tries to return this , I get this exception :

May 14, 2015 9:54:57 AM org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo SEVERE: MessageBodyWriter not found for media type=application/json, type=class com.fms.core.FMSResponseInfo, genericType=class com.fms.core.FMSResponseInfo.

I have included jackson-annotation-2.4.2.jar, jackson-core-2.4.2.jar and jackson-databind-2.4.2.jar. These are the three jackson jars I have included along with the other jars required by jersey 2.16 and hibernate and mysql.

Please help me resolve this issue !!

You need more than just Jackson, you need the Jackson JAX-RS provider.

在此处输入图片说明

Then register it either in web.xml

<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>
        your.resource.provider.package,
        com.fasterxml.jackson.jaxrs.json
    </param-value>
</init-param>

Or in your ResourceConfig

packages("your.resource.provider.packgae",
         "com.fasterxml.jackson.jaxrs.json");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM