0

I'm new to Spring Boot and REST API, While I'm saving a row in a mysql table, getting the JSON response with null values for the fields of a mapped table. But the fields have the values in that mapped table.

I'm sending the request as JSON using Postman.

I don't know how to get the response with values for all the fields. Below I mentioned the code.

Controller class:

@RestController
public class HelpDeskController<T> extends RestUtils<T> {
    @Autowired
    HelpDeskService hService;
    @RequestMapping(value = "/helpDesk/createTicket", method = RequestMethod.POST, headers = "Accept=application/json")
        public @ResponseBody Object setTicket(@Valid @RequestBody HelpDesk ticket) {
            try {
                return getSuccessResponse(hService.setTicket(ticket));
            } catch (StudawnException e) {
                return getErrorResponse(e.getMessage());
            }
        }
}

Service class:

@Service
    public class HelpDeskServiceImpl implements HelpDeskService {
        @Autowired
        HelpDeskRepository helpDeskRepository;

        @Override
        public Object setTicket(HelpDesk ticket) throws StudawnException {
            if (ticket == null)
                throw new StudawnException(ErrorCode.NULL_REQUEST);
            else {

                return helpDeskRepository.save(ticket);
            }
        }
}

Model class

@Entity
@Table(name = "Help_Desk")
public class HelpDesk {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ticket_id")
    private int ticketId;

    @JsonBackReference
    @ManyToOne
    @JoinColumn(name = "student_id")
    private Student student;

    @ManyToOne
    @JoinColumn(name = "category_id",nullable=false,updatable=false)
    private Category category;

    @ManyToOne
    @JoinColumn(name = "sub_category_id",nullable=false,updatable=false)
    private SubCategory subCategory;

    @Column(name = "ticket_desc", length = 300)
    private String ticketDesc;

    @Column(name = "status")
    private short status;

    @Column(name = "response", length = 300)
    private String response;

    @Column(name = "date_created", nullable = false, updatable = false)
    private Date dateCreated;

    @Column(name = "date_closed")
    private Date dateClosed;

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public SubCategory getSubCategory() {
        return subCategory;
    }

    public void setSubCategory(SubCategory subCategory) {
        this.subCategory = subCategory;
    }

    public int getTicketId() {
        return ticketId;
    }

    public void setTicketId(int ticketId) {
        this.ticketId = ticketId;
    }

    public String getTicketDesc() {
        return ticketDesc;
    }

    public void setTicketDesc(String ticketDesc) {
        this.ticketDesc = ticketDesc;
    }

    public short getStatus() {
        return status;
    }

    public void setStatus(short status) {
        this.status = status;
    }

    public String getResponse() {
        return response;
    }

    public void setResponse(String response) {
        this.response = response;
    }

    public Date getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    public Date getDateClosed() {
        return dateClosed;
    }

    public void setDateClosed(Date dateClosed) {
        this.dateClosed = dateClosed;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

}

JSON Request in Postman:

{
        "category" : {"categoryId": 1515},
        "subCategory" : {"subCategoryId": 1502},
        "student" : { "studentId":1101},
        "ticketDesc": "Ticket Desc21",
        "status":12,
        "response": "Resp12",
        "dateCreated": "2018-03-12",
        "dateClosed": "2018-04-12"
}

JSON Response:

Getting null values for the fields in the mapped table but not for the field which is used for mapping (Foreign key).

{
    "response": {
        "ticketId": 22,
        "category": {
            "categoryId": 1515,
            "categoryName": null,
            "createdDate": null
        },
        "subCategory": {
            "subCategoryId": 1502,
            "subCategoryName": null,
            "createdDate": null
        },
        "ticketDesc": "Ticket Desc21",
        "status": 12,
        "response": "Resp12",
        "dateCreated": "2018-03-12T00:00:00.000+0000",
        "dateClosed": "2018-04-12T00:00:00.000+0000"
    },
    "status": 200
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Muthukumaran K
  • 53
  • 1
  • 2
  • 7
  • You need to show us your "HelpDesk" class (assuming this is your model for a "ticket"). Spring will try to bind all values from the JSON to the fields of that class and vice versa, so the naming has to be the identical (or configured via a @JSONProperty), so the getters and setters can be used. – Bennett Dams Apr 19 '18 at 12:38
  • @BennettDams Hi, I've added my model class (HelpDesk) Thanks. – Muthukumaran K Apr 19 '18 at 13:21

1 Answers1

0

@ResponseBody tries to convert the return value of it's method to JSON (by default) - as you return a HelpDesk object via hService.setTicket(ticket), Spring maps all fields of the HelpDesk to it's JSON values. In your HelpDesk class, your categoryfield is from type Category, so all fields of the class Category have to be named exactly like the JSON fields for Spring being able to bind them with the getters/setters.

You could alternatively use the @JSONProperty annotation to bind your class fields to the JSON names in your Categoryclass: When is the @JsonProperty property used and what is it used for?

Bennett Dams
  • 6,463
  • 5
  • 25
  • 45
  • I tried this by adding the @JSONProperty to the fields in my Category class, but its not working. Seeking some other solution. Thanks. – Muthukumaran K Apr 20 '18 at 10:32