问题描述
我试图使用jackson返回一个简单的json响应。
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;
}
这是代码。当函数尝试返回FMSResponseInfo时,它是POJO:
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
}
这是pojo,它将是发回来。
This is the pojo which will be sent back.
但是当函数试图返回这个时,我得到了这个例外:
But as soons as the function tries to return this , I get this exception :
我已经包含了jackson-annotation-2.4.2.jar,jackson-core-2.4.2.jar和jackson-databind-2.4.2.jar。这些是我已经包含的三个杰克逊罐以及球衣2.16和hibernate和mysql所需的其他罐子。
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 !!
推荐答案
您需要的不仅仅是杰克逊,还需要Jackson JAX-RS提供商。
You need more than just Jackson, you need the Jackson JAX-RS provider.
然后在web.xml中注册
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>
或者在你的 ResourceConfig
packages("your.resource.provider.packgae",
"com.fasterxml.jackson.jaxrs.json");
这篇关于jackson - 从GET Api返回JSON POJO时找不到MessageBodyWriter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!