【发布时间】:2021-07-05 15:27:19
【问题描述】:
我从一个名为“Person”的对象创建了一个表,我可以从 Postman 发布、删除和放置。我对如何从我在邮递员中输入的 JSON 创建新查询没有任何问题。但是,每当我尝试删除或更新(使用 put)我的查询时,我都会使用 @PathVariable 选择我想要的查询,并使用 @RequestVariable 更新选定的数据。但我希望能够从 JSON 文件中删除或更新。这是我的代码的样子。
人物对象
@Entity
@Table(name="person")
public class Person {
@Id
@SequenceGenerator(
name = "person_sequence",
sequenceName = "person_sequence",
allocationSize = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "person_sequence"
)
@Column(name="id")
private Long id;
private String name;
private String lastname;
}
//Getters, Setters, Constructors and ToString is here
人员控制器
@RestController
@RequestMapping(path = "api/v1/person")
public class PersonController {
private final PersonService personService;
@Autowired
public PersonController(PersonService personService) {
this.personService = personService;
}
//@GetMapping
//@PostMapping
@DeleteMapping(path = "{personId}")
public void deletePerson(@PathVariable("personId") Long personId{
personService.deletePerson(personId);
}
@PutMapping(path = "{personId}")
public void updatePerson(@PathVariable("personId") Long personId,
@RequestParam(required = false) String name,
@RequestParam(required = false) String lastname){
personService.updatePersonService(personId, name, lastname);
}
}
这是我的 PersonRepository 接口
@Repository
public interface PersonRepository extends JpaRepository<Person, Long> {
}
我对 Spring Boot 很陌生。如果我能在@PutMapping 和@DeleteMapping 中得到一个代码,以及代码在PersonService 类中的请求中的样子,那就太好了。
【问题讨论】:
标签: java postgresql spring-boot spring-data-jpa postman