gpt4 book ai didi

java - 为什么 GET 方法返回对象中的对象

转载 作者:行者123 更新时间:2023-12-02 16:35:49 24 4
gpt4 key购买 nike

我有 2 个实体:UserUserDetails
一个用户只能拥有一个用户详细信息。

    /* class User */
@Entity
@Table(name = "users")
public class User {

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "login")
private String login;

@Column(name = "password")
private String password;

@OneToOne(mappedBy = "user", cascade = CascadeType.ALL,
fetch = FetchType.LAZY, optional = false)
private UserDetails userDetails;

/* getters and setters */
}
/* class UserDetails */

@Entity
@Table(name = "userdetails")
public class UserDetails {

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "name")
private String name;

@Column(name = "surname")
private String surname;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

/* getters and setters */
}

这是类UserDetailsController:

@RestController
@RequestMapping("/userDetails")
public class UserDetailsController {
@GetMapping("/{id}")
public UserDetails findById(@PathVariable Long id) {
return userDetailsRepository.findById(id).orElseThrow(() -> new UserDetailsNotFoundException(id));
}

@GetMapping("/")
public Iterable<UserDetails> findAll() {
return userDetailsRepository.findAll();
}

@PostMapping("/")
public UserDetails save(@RequestBody UserDetails userDetails) {
return userDetailsRepository.save(userDetails);
}

@PostMapping(value = "/", params = {"name", "surname", "user_id"})
public UserDetails saveWithParams(@RequestParam("name") String name, @RequestParam("surname") String surname,
@RequestParam("user_id") Long userId) {
UserDetails userDetails = new UserDetails();
userDetails.setName(name);
userDetails.setSurname(surname);
User user = userRepository.findById(userId).orElseThrow(() -> new UserNotFoundException(userId));
userDetails.setUser(user);
return userDetailsRepository.save(userDetails);
}
}

这就是我尝试在 .http 文件中使用我的 Controller 的方式:

GET http://localhost:8080/userDetails/

###

GET http://localhost:8080/userDetails/4

###

POST http://localhost:8080/userDetails/
Content-Type: application/json

{
"name": "my name",
"surname": "my surname",
"user": {
"user_id": 12
}
}

###

POST http://localhost:8080/userDetails/
Content-Type: application/x-www-form-urlencoded

user_id=12&name=testName&surname=testSurname

###

<强>1。为什么第一个 POST 方法不起作用:异常 user_id 不能为空?
2. 为什么userDetails中的GET all方法都返回这样的东西?:

enter image description here

最佳答案

Why first POST method not works: exception user_id can't be null

因为您正在接收数据作为请求参数而不是请求正文。所以所有数据都是空的。所以这就是为什么它给你错误。如果您想作为请求正文发送,则使用这些字段创建一个 DTO 类并使用 @RequestBody

进行映射
public UserDetails saveWithParams(@RequestBody UserDetailsDTO reqestBody) {
...
}

Why both GET all method in userDetails returns something like this?

您正在发送以这种方式定义关系的实体,因此它将递归地获取数据。您可以在 UserDetails 类的 user 上使用 @JsonIgnore 来解决它,这样就不会再次获取用户。

@JsonIgnore
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

关于java - 为什么 GET 方法返回对象中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62740932/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com