对Spring的API调用为Event
实体的courseid
字段返回0,所有其他字段返回正确的值
inEventController
@GetMapping("/events")
Page<EventVM> getEvents(Pageable page) {
return eventService.getEvents(page).map(EventVM::new);
}
EventService
public Page<Event> getEvents(Pageable pageable ) {
return eventRepository.findAll(pageable);
}
然后如果我在邮差中尝试这个请求,我得到
{
"id": 453,
"eventname": "This is new",
"courseid": 0,
"date": "2021-12-12",
"maxentrants": 12,
"cost": 20.00,
"eventtype": "4BBB",
"qualifier": false,
"info": "This is a test from react",
"winner": null
}
所以courseid
显示为0而实际上是241
为了确保我可以得到这个值,我在EventController中创建了一个简单的geteevents,
@GetMapping("management/events")
List<Event> getEvents() {
return eventRepository.findAll();
}
正确返回courseid
中的所有值。
Event
表中的courseid
是Course
表中id
的外键。我认为这可能是问题,但它工作得很好,当我做了简单的getEvents调用。
com.hoaxify.hoaxify.event.vm;
import com.hoaxify.hoaxify.event.Event;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
@Data
@NoArgsConstructor
public class EventVM {
private Long id;
private String eventname;
private long courseid;
private String date;
private int maxentrants;
private BigDecimal cost;
private String eventtype;
private Boolean qualifier;
private String info;
private String winner;
public EventVM(Event event) {
this.setId(event.getEventid());
this.setEventname(event.getEventname());
this.setEventtype(event.getEventtype());
this.setDate(event.getDate());
this.setCost(event.getCost());
this.setInfo(event.getInfo());
this.setQualifier(event.getQualifier());
this.setMaxentrants(event.getMaxentrants());
this.setWinner(event.getWinner());
}
}
假设Event
有courseid
属性,那么您需要修复EventVM
构造函数如下:
public EventVM(Event event) {
this.setId(event.getEventid());
this.setEventname(event.getEventname());
this.setEventtype(event.getEventtype());
this.setCourseid(event.getCourseid());
this.setDate(event.getDate());
this.setCost(event.getCost());
this.setInfo(event.getInfo());
this.setQualifier(event.getQualifier());
this.setMaxentrants(event.getMaxentrants());
this.setWinner(event.getWinner());
}
注意添加的this.setCourseid(event.getCourseid());
行