0

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);
1
  • try with adding this dependency in your pom : <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.10</version> </dependency> And put the @ResponseBody just before the return of your method ( ... @ResponseBody List<EventORM> ...) Commented Mar 31, 2016 at 22:34

1 Answer 1

2

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(i.e 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Bunti for taking time to answer. i have implemented the first approach, helped me a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.