[英]How to let getMapping show data spring boot
I have a vue application where I am making an axios.get request to my backend.我有一个 vue 应用程序,我正在向我的后端发出 axios.get 请求。 I just want the data which is send from the backend to write to the console but it does not work.
我只希望从后端发送的数据写入控制台,但它不起作用。 There is no value returned.
没有返回值。 I tried to do a request through postman the response is 200 but there is no json object shown.
我尝试通过 postman 发出请求,响应为 200,但没有显示 json object。 No errors are shown either I could not find any errors because of that.
没有显示任何错误,因此我找不到任何错误。
Vue axios: Vue axios:
getMeetingTimes(){
axios.get("http://localhost:8081/api/meetingTime/")
.then(function(response){
console.log(response.headers)
})
}
Spring entities: Spring 实体:
MeetingSetting:会议设置:
@Entity
@Table(name = "meeting_times")
@Getter
@Setter
public class MeetingTime implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "meeting_date")
private String date;
@Column(name = "start_time")
private String startTime;
@Column(name = "meeting_name")
private String meeting_name;
@Column(name = "end_time")
private String endTime;
@ManyToOne
@JoinColumn(name = "meeting_name" ,insertable = false, updatable = false, referencedColumnName = "meeting_name")
private MeetingsSetting meetingName;
}
Service:服务:
@Service
public class MeetingTimeService {
@Autowired
MeetingTimeRepository meetingTimeRepository;
public List<MeetingTime> findAllMeetingTimes(){
System.out.println("HALLOOO "+meetingTimeRepository.findAll());
return meetingTimeRepository.findAll();
}
}
this is the log I am getting from intellj:这是我从 intellj 得到的日志:
Hibernate: select meetingtim0_.id as id1_1_, meetingtim0_.meeting_date as meeting_2_1_, meetingtim0_.end_time as end_time3_1_, meetingtim0_.meeting_name as meeting_4_1_, meetingtim0_.start_time as start_ti5_1_ from meeting_times meetingtim0_
Hibernate: select meetingsse0_.id as id1_0_0_, meetingsse0_.meeting_name as meeting_2_0_0_, meetingsse0_.meeting_pw as meeting_3_0_0_, meetingsse0_.meeting_url as meeting_4_0_0_ from meeting_settings meetingsse0_ where meetingsse0_.meeting_name=?
Hibernate: select meetingsse0_.id as id1_0_0_, meetingsse0_.meeting_name as meeting_2_0_0_, meetingsse0_.meeting_pw as meeting_3_0_0_, meetingsse0_.meeting_url as meeting_4_0_0_ from meeting_settings meetingsse0_ where meetingsse0_.meeting_name=?
HALLOOO [com.cbc.coorporateblinddateservice.entities.dates.MeetingTime@4b385665, com.cbc.coorporateblinddateservice.entities.dates.MeetingTime@5afe0ab6, com.cbc.coorporateblinddateservice.entities.dates.MeetingTime@7a395a1b, com.cbc.coorporateblinddateservice.entities.dates.MeetingTime@22155873, com.cbc.coorporateblinddateservice.entities.dates.MeetingTime@2b17a92a, com.cbc.coorporateblinddateservice.entities.dates.MeetingTime@196ad44, com.cbc.coorporateblinddateservice.entities.dates.MeetingTime@314fd6ef]
Hibernate: select meetingtim0_.id as id1_1_, meetingtim0_.meeting_date as meeting_2_1_, meetingtim0_.end_time as end_time3_1_, meetingtim0_.meeting_name as meeting_4_1_, meetingtim0_.start_time as start_ti5_1_ from meeting_times meetingtim0_
UPDATE更新
Controller: Controller:
@CrossOrigin(origins = "http://localhost:8080")
@RestController
@RequestMapping("api/meetingTime")
public class MeetingTimeController {
@Autowired
MeetingTimeService meetingTimeService;
@GetMapping(value = "/" )
public void getAllTimes(){
meetingTimeService.findAllMeetingTimes();
}
}
Save method for meetingSettings and meetingTime: meetingSettings 和 meetingTime 的保存方法:
@PostMapping("/")
public void saveMeeting(@RequestBody MeetingsSetting meetingsSetting){
meetingsSetting.getMeetingTime()
.forEach(mt -> mt.setMeeting_name(meetingsSetting.getMeetingName()));
// ...
meetingSettingService.saveMeeting(meetingsSetting);
}
}
this is inside meetingSettingCOntroller这是在会议设置控制器里面
This is the outcome:这是结果:
{
"id": 35,
"date": "2021-06-21",
"startTime": "11:01",
"meeting_name": "Testeriuds",
"endTime": "11:01",
"meetingName": {
"id": 17,
"meetingName": "Testeriuds",
"meetingUrl": null,
"meetingPw": "",
"meetingTime": [
{
"id": 37,
"date": "2021-05-31",
"startTime": "15:30",
"meeting_name": "Testeriuds",
"endTime": "16:30",
THis error comes when I make the request from axios.当我从 axios 发出请求时出现此错误。 But still shows data on the console:
但仍然在控制台上显示数据:
java.lang.StackOverflowError: null
at java.base/java.io.IOException.<init>(IOException.java:81) ~[na:na]
at com.fasterxml.jackson.core.JacksonException.<init>(JacksonException.java:26) ~[jackson-core-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.core.JsonProcessingException.<init>(JsonProcessingException.java:25) ~[jackson-core-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.core.JsonProcessingException.<init>(JsonProcessingException.java:38) ~[jackson-core-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.JsonMappingException.<init>(JsonMappingException.java:245) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:785) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:770) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178) ~[jackson-databind-2.12.3.jar:2.12.3]
Have your @CrossOrigin
match the host requested by axios.让您的
@CrossOrigin
匹配 axios 请求的主机。 Also make your controller endpoint return something, not just call the method:还要让您的 controller 端点返回一些东西,而不仅仅是调用方法:
@CrossOrigin(origins = "http://localhost:8081") // <-- because axios did 8081
@RestController
@RequestMapping("api/meetingTime")
public class MeetingTimeController {
@Autowired
MeetingTimeService meetingTimeService;
@GetMapping(value = "/" )
public List<MeetingTime> getAllTimes(){ // removed void return type
return meetingTimeService.findAllMeetingTimes(); // <-- added return here
}
}
So the final error I had is called infinite recursion and can happen in Bidirectional Entity Relationship.所以我遇到的最后一个错误被称为无限递归,并且可能发生在双向实体关系中。 One fix I had found was to put the @JsonManagedReference in the Entity which has the ManytoOne annotation in my case
我发现的一个解决方法是将@JsonManagedReference 放在具有 ManytoOne 注释的实体中
MeetingSetting was looking like this: MeetingSetting 看起来像这样:
@JsonManagedReference
@OneToMany(mappedBy = "meetingName", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<MeetingTime> meetingTime = new HashSet<>();
And meetingTime is like this:会议时间是这样的:
@JsonBackReference
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "meeting_name" ,insertable = false, updatable = false, referencedColumnName = "meeting_name")
private MeetingsSetting meetingName;
Maye there is a better solution and I would be grateful if someone would tell me if my solution is correct or a bad choice. Maye 有一个更好的解决方案,如果有人能告诉我我的解决方案是正确的还是错误的选择,我将不胜感激。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.