英文:
Hibernate respond on Post-mapping method with Spring boot returning null after sending request to MySQL database
问题
在破坏了编程后,使用Postman进行API的客户端测试时,从控制器返回的响应数据一直都是Null Null,代码看起来一切都正常,没有错误消息或提示,但是Hibernate一直返回null null,并且没有从MySQL数据库中返回数据(请看下面的代码,帮忙解决)。
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Data
@Table(name = "student_lib")
public class StudentLibrary {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String addressess;
private String email;
// 省略了getter和setter方法,构造函数等...
}
@Service
public class LibService {
@Autowired
private LibRepository repository;
public StudentLibrary getLibraryById(Long id) {
return repository.findById(id).get();
}
public StudentLibrary insertLibrary(StudentLibrary studentLibrary) {
return repository.save(studentLibrary);
}
}
@RestController
@RequestMapping("/Api")
public class LibController {
@Autowired
LibService service;
@GetMapping("/Lib")
public StudentLibrary getAllDataById(Long id) {
return service.getLibraryById(id);
}
@PostMapping("/save")
public StudentLibrary insertData(StudentLibrary studentLibrary) {
return service.insertLibrary(studentLibrary);
}
}
Postman响应结果:
{
"id": 12,
"name": null,
"addressess": null,
"email": null
}
希望这可以帮助你解决问题。如果问题仍然存在,请提供更多关于问题的详细信息以获取更多帮助。
英文:
After ruining the programming, using Postman for client testing with the Api, from the controller, the response data keep returning Null Null
everything seems fine with the code no error messages or prompt , but Hibernate keep returning null null and no data which post is inserted in the MySQL database (code below ,please help)
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Data
@Table(name = "student_lib")
public class StudentLibrary {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String addressess;
private String email;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddressess() {
return addressess;
}
public void setAddressess(String addressess) {
this.addressess = addressess;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public StudentLibrary(Long id, String name, String addressess, String email) {
super();
this.id = id;
this.name = name;
this.addressess = addressess;
this.email = email;
}
Service
@Service
public class LibService {
@Autowired
private LibRepository repository;
public StudentLibrary getLibraryById(Long id) {
return repository.findById(id).get();
}
public StudentLibrary insertLibrary(StudentLibrary studentLibrary) {
return repository.save(studentLibrary);
}
}
Controller
@RestController
@RequestMapping("/Api")
public class LibController {
@Autowired
LibService service;
@GetMapping("/Lib")
public StudentLibrary getAllDataById(Long id) {
return service.getLibraryById(id);
}
@PostMapping("/save")
public StudentLibrary insertData(StudentLibrary studentLibrary) {
return service.insertLibrary(studentLibrary);
}
}
outcome respond with postman
{
"id": 12,
"name": null,
"addressess": null,
"email": null
}
答案1
得分: 2
你需要在POST API的StudentLibrary参数之前添加@RequestBody注解,否则它会被视为HTTP请求体。
对于GET请求,如果通过URL传递Long类型的id参数,你应该使用@PathVariable("id")并将URL修改为@GetMapping("/Lib/{id}")(包括括号)。
英文:
You are missing @RequestBody annotation before the StudentLibrary parameter of your POST api, otherwise it will be taken by HTTP body.
About the GET one, if that Long id is passed through URL, you should use @PathVariable("id") and modify the url as @GetMapping("/Lib/{id}") (brackets included)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论