我是Spring Boot和REST API的新手,当我在mysql表中保存一行时,使用映射表的字段的空值获取JSON响应。但是这些字段具有该映射表中的值。
我使用Postman以JSON的形式发送请求。
我不知道如何使用所有字段的值获取响应。下面我提到了代码。
控制器类:
@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
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);
}
}
}
模型类
@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请求:
{
"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": {
"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
}
答案 0 :(得分:0)
@ResponseBody
尝试将其方法的返回值转换为JSON(默认情况下) - 当您通过HelpDesk
返回hService.setTicket(ticket)
对象时,Spring会映射{{1}的所有字段它的JSON值。在HelpDesk
课程中,您的HelpDesk
字段来自category
类型,因此类Category
的所有字段都必须与Spring的JSON字段完全相同用getter / setter绑定它们。
您也可以使用Category
注释将类字段绑定到@JSONProperty
类中的JSON名称:When is the @JsonProperty property used and what is it used for?