【发布时间】:2020-04-06 12:11:13
【问题描述】:
我对 API 完全陌生,最近使用 Spring Boot 开发了我的第一个 API。到目前为止,我已经实现了将 h2 内存数据库中的所有记录作为 Json 列表检索,但是因为我将尝试使用 React 获取它们并使用 D3 在树形图中显示它们,所以我还需要获取它们采用嵌套的 JSON 格式。
这是我目前的代码:
外星人.java
package com.example.alien.model;
import java.util.List;
import java.util.Optional;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
public class Alien {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String type;
private String planet;
@JsonIgnore
@ManyToOne(targetEntity = Alien.class, fetch=FetchType.LAZY)
@JoinColumn(name="parentId")
private Optional<Alien> parent;
@JsonIgnore
@OneToMany(targetEntity = Alien.class, cascade=CascadeType.ALL, mappedBy="parent", fetch=FetchType.LAZY)
private List<Alien> children;
public List<Alien> getChildren() {
return children;
}
public void setChildren(List<Alien> children) {
this.children = children;
}
public Optional<Alien> getParent() {
return parent;
}
public void setParent(Optional<Alien> parent) {
this.parent = parent;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPlanet() {
return planet;
}
public void setPlanet(String planet) {
this.planet = planet;
}
}
AlienDto.java
package com.example.aliendto;
import java.util.Optional;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import com.example.alien.model.Alien;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class AlienDto {
private String name;
private String type;
private String planet;
private Integer parentId = 0;
public String getName() {
return name;
}
public String getType() {
return type;
}
public String getPlanet() {
return planet;
}
public Integer getParentId() {
return parentId;
}
}
AlienController.java
package com.example.alien.controller;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.alien.dao.AlienRepo;
import com.example.alien.model.Alien;
import com.example.aliendto.AlienDto;
@CrossOrigin(origins = "*", allowedHeaders = "*")
@RestController
public class AlienController
{
@Autowired
AlienRepo repo;
@PutMapping("/alien/{id}")
public Alien updateAlien(@RequestBody Alien alien) {
repo.save(alien);
return alien;
}
@DeleteMapping("/alien/{id}")
public String deleteAlien(@PathVariable Integer id)
{
Alien a = repo.getOne(id);
repo.delete(a);
return "deleted";
}
@PostMapping("/alien")
public Alien addAlien(@RequestBody AlienDto dto) {
Alien alien = new Alien();
alien.setName(dto.getName());
alien.setType(dto.getType());
alien.setPlanet(dto.getPlanet());
if(dto.getParentId() == 0) {
alien.setParent(null);
}else {
Optional<Alien> parent = this.repo.findById(dto.getParentId());
alien.setParent(parent);
}
repo.save(alien);
return alien;
}
@GetMapping("/aliens")
public List<Alien> getAliens() {
return repo.findAll();
}
@RequestMapping("/alien/{id}")
public Optional<Alien> oneAlien(@PathVariable("id") Integer id) {
return repo.findById(id);
}
@GetMapping("/")
public String home()
{
return "do_Ob";
}
}
AlienRepo.java
package com.example.alien.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.alien.model.Alien;
public interface AlienRepo extends JpaRepository<Alien, Integer>
{
}
现在我在 localhost:8080/aliens 上得到这样的结果
[{"id":1,"name":"Javier","type":"Alpha","planet":"Earth","children":"Laia"},
{"id":2,"name":"Javier","type":"Alpha","planet":"Earth","children":"John"},
{"id":3,"name":"Laia","type":"Omega","planet":"Earth","children":""},
{"id":4,"name":"John","type":"Omega","planet":"Earth","children":""}]]
但我也想让他们像这样走另一条路:
[{"name":"Javier",
"type":"Alpha",
"planet":"Earth",
"children":[{"name": "Laia", "type":"Omega",....},
{"name": "John", "type":"Omega",....}]]
如果我可以使用 React 将 JSON 列表转换为嵌套的 Json 也可以。
谢谢
编辑:
https://i.imgur.com/zuc8D6I.png - 邮递员输出
【问题讨论】:
标签: java reactjs spring-boot rest