Hibernate respond on Post-mapping method with Spring boot returning null after sending request to MySQL database

huangapple go评论82阅读模式
英文:

Hibernate respond on Post-mapping method with Spring boot returning null after sending request to MySQL database

问题

在破坏了编程后,使用Postman进行客户端测试时,从控制器返回的响应数据始终为空。

代码看起来没有问题,没有错误消息或提示,但是Hibernate返回的是空值,没有数据插入到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方法

    public StudentLibrary(Long id, String name, String addressess, String email) {
        super();
        this.id = id;
        this.name = name;
        this.addressess = addressess;
        this.email = email;
    }
}

@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 API,如果通过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)

huangapple
  • 本文由 发表于 2023年8月9日 14:28:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定