我使用@Produces(MediaType.APPLICATION_JSON)返回JSON,但是它将以下信息作为Java对象而不是正确的JSON返回
任何想法我如何获得正确的数据,而不是班级或列表信息?
期望格式
{“ page”:1,“ pageSize”:20,“ totalResults”:4547,“ sortField”:“ logId”,“ sortDirection”:“ desc”,“ list”:[{“ logId”:4548,“ loggingFor“:” MAXIMO_ACCNT“,” loggedName“:” AdamNev“,”详细信息“:”:Mozilla /“,” loggedDate“:1588394266000,” totalCount“:null}]}
生成的输出
{“列表”:[“ mycompany.class.LogUtil@2bd24315”,“ mycompany.class.LogUtil@5b71cbba”,“ mycompany.class.LogUtil@7679315d”,“ mycompany.class.LogUtil@2591e588”,“ mycompany .class.LogUtil @ 1ce2c0c4“,” mycompany.class.LogUtil@499be4f9“,” mycompany.class.LogUtil@7657d74e“,” mycompany.class.LogUtil@5c40d1a9“,” mycompany.class.LogUtil@562ede05“,” mycompany。 .class.LogUtil @ 40845415“,” mycompany.class.LogUtil@82106ec“,” mycompany.class.LogUtil@52b643d8“,” mycompany.class.LogUtil@72c8717d“,” mycompany.class.LogUtil@100ea7cd“,” mycompany .class.LogUtil @ 1098c546“,” mycompany.class.LogUtil@7050ec0e“,” mycompany.class.LogUtil@412238e0“,” mycompany.class.LogUtil@24f7cfc6“,” mycompany.class.LogUtil@72939ceb“,” mycompany .class.LogUtil @ 5543e2b“],” page“:1,” pageSize“:20,” sortDirection“:” desc“,” sortField“:” logId“,” totalResults“:4547}]
从数据库中获取数据的Java类代码片段
GET
@Path("/list")
@Produces(MediaType.APPLICATION_JSON)
public LogDataWrapper getLogs(@Context UriInfo uriInfo) throws JSONException {
LogDataWrapper<logDataUtil> logDataWrapper =
new LogDataWrapper<logDataUtil>();
try {
MultivaluedMap params = uriInfo.getQueryParameters();
StringBuilder builder = new StringBuilder();
for (Object key : params.keySet()) {
builder.append(key)
.append(":")
.append(params.getFirst(key))
.append("\n");
}
JSONObject inputJSON = new JSONObject(builder.toString().substring(builder.toString().indexOf('{')));
Integer page = inputJSON.getInt("page"); //setting parameter from .JS file
Integer pageSize = inputJSON.getInt("pageSize"); //setting parameter from .JS file
if (inputJSON.has("sort")) {
JSONArray sorts = inputJSON.getJSONArray("sort");
for (int i = 0; i < sorts.length(); i++) {
JSONObject jo = (JSONObject) sorts.get(i);
logDataWrapper.setSortField(jo.getString("field")); //setting parameter from .JS file
logDataWrapper.setSortDirection(jo.getString("dir")); //setting parameter from .JS file
}
}
logDataWrapper.setPage(page); //setting parameter from .JS file
logDataWrapper.setPageSize(pageSize); //setting parameter from .JS file
} catch (Exception e) {
log.error("Exce " + e.getMessage());
e.printStackTrace();
}
return getLogs(logDataWrapper); the below is the method
private LogDataWrapper<logDataUtil> getLogs(LogDataWrapper<logDataUtil> wrapper) throws JSONException {
//wrapper.setTotalResults(countLog());
wrapper.setList(getLogsList(wrapper)); // the below gets data from database
return wrapper;
}
private List<logDataUtil> getLogsList(LogDataWrapper<logDataUtil> wrapper)
throws JSONException {
List<logDataUtil> result = null;
try {
CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<logDataUtil> criteriaQuery =
criteriaBuilder.createQuery(logDataUtil.class);
Root<MyLog> root = criteriaQuery.from(MyLog.class);
Join<MyLog, Employee> joinLoggedFor = root.join(MyLog_.employee, JoinType.LEFT);
Join<MyLog, Employee> joinLoggedBy = root.join(MyLog_.employee1, JoinType.LEFT);
Join<MyLog, ApplicationList> joinAppName =
root.join(MyLog_.applicationList, JoinType.LEFT);
criteriaQuery.multiselect(root.get(MyLog_.logId), joinLoggedFor.get(Employee_.employeeNumber),
criteriaBuilder.selectCase().when(criteriaBuilder.equal(joinLoggedFor.get(Employee_.employeeNumber),
"Self Reset"),
"Self Reset").otherwise(joinLoggedFor.get(Employee_.longName)),
TypedQuery<logDataUtil> typedQuery = getEntityManager().createQuery(criteriaQuery);
int start = (wrapper.getPage() - 1) * wrapper.getPageSize();
typedQuery.setFirstResult((start));
typedQuery.setMaxResults(wrapper.getPageSize());
result = typedQuery.getResultList();
} catch (Exception e) {
log.error("Exception from getPasswordResetLogsList " + e.getMessage());
}
return result;
}
请参阅:https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/message-body-workers.html