[英]how to receive a json object with @RequestBody or @RequestParam
首先我很抱歉,我是使用這項技術的新手,我真的有很多疑問。 I am trying to send a json object to my controller class, the problem is that with @RequestBody
all the data arrives but the foreign keys arrive null
. 示例輸入一個新用戶,其角色ID已經存在於BD中,用戶數據到達完成但角色ID到達null
首先,我通過 POST 注冊角色的數據,並且一切都與@RequestBody
完美配合,但是當我嘗試使用已經保存在數據庫中的role_id
注冊用戶時,也使用@RequestBody
它與id_rol
null
保存
我的用戶實體:
@Entity
public class Usuario implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String correo;
private String pass;
private boolean activo;
@OneToOne(optional = false, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="rol_id", nullable = false)
private Rol rol;
public Usuario(){}
public Usuario(String correo, String pass, boolean activo, Rol rol) {
this.correo = correo;
this.pass = pass;
this.activo = activo;
this.rol = rol;
}
/*getter y setter*/
我的角色實體:
@Entity
//@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Rol implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String nombre;
private boolean activo;
@OneToMany(mappedBy = "rol")
private List<Usuario> usuarios;
public Rol(){}
public Rol(String nombre, boolean activo, List<Usuario> usuarios) {
this.nombre = nombre;
this.activo = activo;
this.usuarios = usuarios;
}
/*getter y setter*/
用戶存儲庫:
@Repository
public interface UsuarioRepository extends JpaRepository<Usuario, Serializable> {
public abstract Usuario findByCorreo(String correo);
}
角色庫:
public interface RolRepository extends JpaRepository<Rol, Serializable> {
}
用戶 controller class:
@RestController
@RequestMapping("/v1")
public class UsuarioController {
@Autowired
UsuarioRepository usuarioRepository;
@PostMapping("/usuario/add")
public @ResponseBody ResponseEntity<String> crear(@Valid @RequestBody Usuario usuario) {
try{
usuarioRepository.save(usuario);
return new ResponseEntity<String>("Registro Exitoso..", HttpStatus.OK);
}catch (Exception e){
return new ResponseEntity<String>("Ha ocurrido un Error..", HttpStatus.BAD_REQUEST);
}
}
}
角色 controller class:
@RestController
@RequestMapping("/v1")
public class RolController {
@Autowired
RolRepository rolRepository;
@PostMapping("/rol/add")
public @ResponseBody ResponseEntity<String> crear(@Valid @RequestBody Rol rol) {
try{
rolRepository.save(rol);
return new ResponseEntity<String>("Registro Exitoso..", HttpStatus.OK);
}catch (Exception e){
return new ResponseEntity<String>("Ha ocurrido un Error..", HttpStatus.BAD_REQUEST);
}
}
}
這是我發送給我的 controller 的內容:
{
"correo": "pepito@gmail.com",
"pass": "1234",
"activo": "true",
"rol_id": "5"
}
這就是我收到的:
{
"correo": "pepito@gmail.com",
"pass": "1234",
"activo": true,
"rol_id": null
}
我應該如何接收這個 id_rol = 5 的身體? 我知道我在@RequestBody
做錯了什么,我很感激你可以提供的任何例子,因為我已經搜索過,但我沒有找到。 謝謝..!
這是因為您向 REST Controller 發送了錯誤的 JSON 結構。
在您的Usuario
中, role_id
實際上是一個 object 字段,其名稱為rol
,代表Rol
class。 因此,您需要在請求中將帶有id
的Rol
作為 JSON object 傳遞:
{
"correo": "pepito@gmail.com",
"pass": "1234",
"activo": "true",
"rol": {
"id":"5"
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.