简体   繁体   中英

Getting null values for the other fields of a mapped table in JSON response (Spring Boot, REST)

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
}

@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 category field 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 Category class: When is the @JsonProperty property used and what is it used for?

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