我对API完全陌生,最近使用Spring Boot开发了我的第一个API。到目前为止,我已经实现了将h2内存数据库中的所有记录作为Json的列表进行检索,但是由于我将尝试使用React来获取它们并使用D3在树状图中显示它们,因此我还需要获取它们以嵌套的JSON格式。
到目前为止,这是我的代码:
Alien.java
package com.example.alien.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Alien {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String type;
private String planet;
private String children;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
public String getChildren() {
return children;
}
public void setChildren(String children) {
this.children = children;
}
}
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;
@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 Alien alien) {
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,也可以。
谢谢
[而不是将子代作为字符串,而是将它们作为对象列表。替换为:
您需要进行一些更改以使其具有层次结构,请检查以下内容: