Я возвращаю JSON, используя @Produces (MediaType.APPLICATION_ JSON), однако он возвращает приведенную ниже информацию в виде Java объекта вместо правильного JSON
Любая идея, как я могу получить правильные данные вместо информации о классе или списке?
Ожидаемый формат
{"page": 1, "pageSize": 20, "totalResults": 4547, "sortField": "logId", "sortDirection": "des c", "list": [{"logId": 4548, "loggedFor": "MAXIMO_ACCNT", "loggedName": "Adam Nev", "details": ": Mozilla / "," loggedDate ": 1588394266000," totalCount ": null}]}
Сгенерировано вывода
{" list ": [" 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 "" MYCOMP any.class LogUtil@412238e0», "mycompany.class LogUtil@24f7cfc6", "mycompany.class LogUtil@72939ceb", "mycompany.class LogUtil@5543e2b"], "стр....": 1, "PAGESIZE": 20, "sortDirection": "des c", "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;
}