简体   繁体   中英

Spring Rest Api Json Response not displaying property names

I am new to Spring and I am dealing with strange behavior when I get the json response from API, I do not get property names, just the values. How can I possibly find what is preventing propertyName to return in response?

Current Response:

  [
  [
    "6ED569AAE51C401CB621E96856766BF4",
    "{\"EventType\":\"Test\",\"lastName\":\"Smith\",\"age\":25,\"address\":{\"streetAddress\":\"212ndStreet\",\"city\":\"NewYork\",\"state\":\"NY\",\"postalCode\":\"10021\"}}",
    "{\"EventType\":\"Test\",\"lastName\":\"Smith\",\"age\":25,\"address\":{\"streetAddress\":\"212ndStreet\",\"city\":\"NewYork\",\"state\":\"NY\",\"postalCode\":\"10021\"}}",
    "JPA_USER",
    1459372275949,
    "JPA_USER",
    1459372275949,
    "A",
    "UPJPATESTEVENT",
    "8210D1C62E014F158859EC0D034435BC"
  ]
]

However the correct response will be with property names like:

[
  [
    "refEventTypeId":"6ED569AAE51C401CB621E96856766BF4",
    "jsonExampleDocument":"{\"EventType\":\"Test\",\"lastName\":\"Smith\",\"age\":25,\"address\":{\"streetAddress\":\"212ndStreet\",\"city\":\"NewYork\",\"state\":\"NY\",\"postalCode\":\"10021\"}}",
    "jsonAvroSchema":"{\"EventType\":\"Test\",\"lastName\":\"Smith\",\"age\":25,\"address\":{\"streetAddress\":\"212ndStreet\",\"city\":\"NewYork\",\"state\":\"NY\",\"postalCode\":\"10021\"}}",
    "createUser":"JPA_USER",
    "createDate":1459372275949,
    "updateUser":"JPA_USER",
    "updateDate":1459372275949,
    "status":"A",
    "eventType":"UPJPATESTEVENT",
    "tenantId":"8210D1C62E014F158859EC0D034435BC"
  ]
]

Controller:

@ResponseBody
@RequestMapping(value = "/abc", method = RequestMethod.GET, produces = "application/json")
public List<EventORM> getAllEvents2(@RequestParam(value = "status", required = false) String status) throws SQLException {                          

    List<EventORM> event = eventManager.getAllEvents(status);

    return event;   
}

I also tried using ResponseEntity in controller but I still get the same result:

@ResponseBody
    @RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<List<EventORM>> getAllEvents(@RequestParam(value = "status", required = false) String status) throws SQLException {                           

        List<EventORM> event = eventManager.getAllEvents(status);

        return new ResponseEntity<List<EventORM>>(event, HttpStatus.OK);   
    }

DAOImpl:

public List<EventORM> getAllEvents(String status) {

    StringBuilder sql = new StringBuilder();
    sql.append("SELECT refEventTypeId, jsonExampleDocument, jsonAvroSchema, createUser, createDate, updateUser, updateDate, status, eventType, tenantId FROM EventORM event");

    Query queryEvents = entityManager.createQuery(sql.toString());          

    return queryEvents.getResultList(); 
}

Model:

package com.epsilon.al.ml.dao.orm.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import com.epsilon.al.ml.commons.AuditModelORM;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

@Entity
@Table(name = "T_REF_EVENT_TYPE", schema = "MI_DEV_USER")
public class EventORM extends AuditModelORM implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 471512149777116797L;

    @Column(name = "REF_EVENT_TYPE_ID")
    @Id
    private String refEventTypeId;

    @Column(name = "JSON_EXAMPLE_DOCUMENT") 
    private String jsonExampleDocument; 

    @Column(name = "JSON_AVRO_SCHEMA")  
    private String jsonAvroSchema;      

    @Column(name = "EVENT_TYPE")
    private String eventType;

    @Column(name = "TENANT_ID")
    private String tenantId; 

    public String getEventType() {
        return eventType;
    }

    public void setEventType(String eventType) {
        this.eventType = eventType;
    }

    public String getTenantId() {
        return tenantId;
    }

    public void setTenantId(String tenantId) {
        this.tenantId = tenantId;
    }

    public String getRefEventTypeId() {
        return refEventTypeId;
    }

    public void setRefEventTypeId(String refEventTypeId) {
        this.refEventTypeId = refEventTypeId;
    }

    public String getJsonExampleDocument() {
        return jsonExampleDocument;
    }

    public void setJsonExampleDocument(String jsonExampleDocument) {
        this.jsonExampleDocument = jsonExampleDocument;
    }

    public String getJsonAvroSchema() {
        return jsonAvroSchema;
    }

    public void setJsonAvroSchema(String jsonAvroSchema) {
        this.jsonAvroSchema = jsonAvroSchema;
    }

    @Override
    public String toString() {
        return "EventORM [refEventTypeId=" + refEventTypeId + ", jsonExampleDocument=" + jsonExampleDocument
                + ", jsonAvroSchema=" + jsonAvroSchema + ", createUser=" + getCreateUser() + ", createDate=" + getCreateDate()
                + ", updateUser=" + getUpdateUser() + ", updateDate=" + getUpdateDate() + ", status=" + getStatus() + ", eventType="
                + eventType + ", tenantId=" + tenantId + "]";
    }

}

Thanks!

Answer in my case:

TypedQuery<EventORM> queryEvents = entityManager.createQuery(sql.toString(), EventORM.class);

Your problem lies in the below code,

StringBuilder sql = new StringBuilder();
sql.append("SELECT refEventTypeId, jsonExampleDocument, jsonAvroSchema, createUser, createDate, updateUser, updateDate, status, eventType, tenantId FROM EventORM event");
Query queryEvents = entityManager.createQuery(sql.toString()); 

With HQL you're now selecting specific fields so that the result will be returned as a list of values corresponding to these fields. This is same as converting a list of strings.

Solution is to,

Specify type in createQuery method as below,

sql.append("SELECT event FROM EventORM event");
return entityManager.createQuery(sql.toString(), EventORM.class);

Or if you don't want to select only a specific set fields

create a new POJO(ie EventDomDTO in one of your suitable DTO package) and modify the query to use EventDomDTO with your selected fields

sql.append("SELECT new com.epsilon.al.ml.dao.orm.EventDomDTO(refEventTypeId, jsonExampleDocument, jsonAvroSchema, createUser, createDate, updateUser, updateDate, status, eventType, tenantId) FROM EventORM event");
return entityManager.createQuery(sql.toString(), EventDomDTO.class);

Note that with the second approach you'll have to change method signature for the getAllEvents

public List<EventDomDTO> getAllEvents(String status)

as now you're returning a custom POJO class.

Hope this will help you.

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