简体   繁体   中英

MessageBodyWriter not found for media type=application/json for DTO with @xmlrootelement

I'm trying to understand in what scenarios (if possible) does one get this type of exception:

MessageBodyWriter not found for media type=application/json for DTO with @xmlrootelement

Yes, my maven dependencies are correct and I even have two similar DTOs that work just fine letting Jersey handle the content negotiation/marshalling...

For some reason, I get the previous mentioned exception with the following:

Resource method:

@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@WebinarSecurityFilter
public Response getXYZ( @QueryParam("startDate") LocalDate start, 
                                    @QueryParam("endDate") LocalDate end, 
                                    @QueryParam("excludesubaccounts") String excludeSubAcc){

    LocalDate now = LocalDate.now();
    LocalDate startDate = start == null ? now.minusDays(30) : start;
    LocalDate endDate = end == null ? now : end;
    boolean excludeSubAccounts = excludeSubAcc != null  && "Y".equalsIgnoreCase(excludeSubAcc);


    List<ABC> resultQuery = abcService.getABCs(clientId, startDate, endDate, excludeSubAccounts);

    CLA cla= new CLA();

    cla.setId(clientId);
    cla.setA(resultQuery);
    cla.setB(resultQuery.size());

    return Response.ok(cla).build();
}

DTO/DTOContainer:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class CLA{

    private long id;
    private long a;
    private long b;

    @XmlElement(name = "abc")
    private List<ABC> ABCs;
//setters & getters
}

DTO:

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class ABC{

// a lot of fields
//setters & getters
}

I've used this same exact scenario with other DTOs, so why is this one not working ?

My question is, are there specific scenarios where Jersey/Jackson aren't able to to this task and one has to do it manually or am I missing something and just have blindness ?

Here's the exception:

MessageBodyWriter not found for media type=application/json, type=class com.CLA, genericType=class com.CLA

Its not that it sometimes works, and sometimes it doesn't, it just doesn't work for this resource method even though I have two other resource methods that do the same exact thing but with different DTOs

Could you give more specific situations when the error happen? Since I just see you declare @Produces annotations but there is no @Consumes annotations I think this error happen when you send a request? If this is the case, just add this either on your desire method or just put it above the class name:

@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })

But if this is not the case, maybe is because you need to specify that you want to send request and receive response in json, so add this in your request header:

Content-Type: application/json
Accept: application/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