主要物體:它使用與“animal_passport”表的一對一關系。一切都是按照教科書做的,應該可以很好地作業。但是由于某種原因,兩個表在呼叫 get 方法時會相互參考。
package com.testapp.model;
import javax.persistence.*;
@Entity
@Table(name = "animal")
public class Animal {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "animal_id", nullable = false, updatable = false)
private long id;
@Column(name = "animal_value")
private String animal_value;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "pass_id")
private PassportAnimal passportAnimal;
public Animal(String animal_value, PassportAnimal passportAnimal) {
this.animal_value = animal_value;
this.passportAnimal = passportAnimal;
}
public Animal() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getAnimal_value() {
return animal_value;
}
public void setAnimal_value(String animal_value) {
this.animal_value = animal_value;
}
public PassportAnimal getPassportAnimal() {
return passportAnimal;
}
public void setPassportAnimal(PassportAnimal passportAnimal) {
this.passportAnimal = passportAnimal;
}
}
附屬物體:
package com.testapp.model;
import javax.persistence.*;
@Entity
@Table(name = "pass_anim")
public class PassportAnimal {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "pass_id", nullable = false,updatable = false)
private long id;
@Column(name = "value_pass")
private String value;
@OneToOne(mappedBy = "passportAnimal")
private Animal animal;
public PassportAnimal(String value, Animal animal) {
this.value = value;
this.animal = animal;
}
public PassportAnimal() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public Animal getAnimal() {
return animal;
}
public void setAnimal(Animal animal) {
this.animal = animal;
}
}
控制動物物體:
package com.testapp.controller;
import com.testapp.model.Animal;
import com.testapp.repository.AnimalRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@CrossOrigin(origins = "http://localhost:8080")
@RestController
@RequestMapping(path = "/api")
public class AnimalController {
@Autowired
AnimalRepository animalRepository;
@GetMapping("/animal")
public ResponseEntity<List<Animal>> getAllAnimal(@RequestParam(required = false) String value)
{
try {
List<Animal> animals = new ArrayList<>();
if (value == null) {
animalRepository.findAll().forEach(animals::add);
}
if (animals.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(animals, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/animal/{id}")
public ResponseEntity<Animal> getAnimalById(@PathVariable("id") long id) {
Optional<Animal> animalOptional = animalRepository.findById(id);
if (animalOptional.isPresent()) {
return new ResponseEntity<>(animalOptional.get(), HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@PostMapping("/animal")
public ResponseEntity<Animal> createAnimal(@RequestBody Animal animal) {
try {
Animal _animal = animalRepository.save(new
Animal(animal.getAnimal_value(),animal.getPassportAnimal()));
return new ResponseEntity<>(_animal, HttpStatus.CREATED);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PutMapping("/animal/{id}")
public ResponseEntity<Animal> updateAnimal(@PathVariable("id") long id, @RequestBody Animal
animal) {
try {
Optional<Animal> animalOptional = animalRepository.findById(id);
if (animalOptional.isPresent()) {
Animal _animal = animalOptional.get();
_animal.setAnimal_value(animal.getAnimal_value());
_animal.setPassportAnimal(animal.getPassportAnimal());
return new ResponseEntity<>(animalRepository.save(_animal), HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@DeleteMapping("/animal/{id}")
public ResponseEntity<HttpStatus> deleteAnimal(@PathVariable("id") long id) {
try {
animalRepository.deleteById(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@DeleteMapping("/animal")
public ResponseEntity<HttpStatus> deleteAllAnimals() {
try {
animalRepository.deleteAll();
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
呼叫GET方法時,應輸出動物及其護照。但是輸出的是無限的json。在護照內,動物一次又一次地展示他的護照。
uj5u.com熱心網友回復:
您必須使用 Jackson 注釋打破回圈。
一側您可以設定@JsonManagedReference,這將被序列化,另一側您設定@JsonBackReference,這將被忽略。
請在這里找到一個很好的解釋:https : //www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion
uj5u.com熱心網友回復:
您正面臨無限遞回問題。請查看@JsonManagedReference 和@JsonBackReference 以解決此問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/379212.html
