【发布时间】:2021-04-21 04:11:01
【问题描述】:
我正在尝试通过使用以下代码发送以下请求来更改 PerfilRepository 中的 Perfil 对象:
package br.com.bandtec.projetocaputeam.controller;
import br.com.bandtec.projetocaputeam.dominio.*;
import br.com.bandtec.projetocaputeam.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
@RestController
@RequestMapping("/caputeam")
public class CaputeamController {
//USER CLASS
public abstract class Perfil {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "nome")
@NotNull
@Size(min = 5,max = 30)
@Pattern(regexp = "^[a-zA-Z\s]*$", message = "Nome inválido! Digite apenas letras e espaçamento") //Permite apenas letras e espaço
private String nome;
@NotNull
@CPF
private String cpf;
@Column(name = "email")
@NotNull
@Email
private String email;
@NotNull
@Size(min = 5,max = 12)
private String senha;
private Integer telefone;
@DecimalMin("0")
@DecimalMax("5")
private Double avaliacao;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "id_endereco")
private Endereco endereco;
@NotNull
private String tipoPerfil;
@OneToOne(cascade = CascadeType.ALL)
private Categoria categoriaAutonomo;
//Getters
public Integer getId() {
return id;
}
public String getNome() {
return nome;
}
public String getCpf() {
return cpf;
}
public String getEmail() {
return email;
}
public String getSenha() {
return senha;
}
public Integer getTelefone() {
return telefone;
}
public Double getAvaliacao() {
return avaliacao;
}
public Endereco getEndereco() {
return endereco;
}
public String getTipoPerfil() {
return tipoPerfil;
}
public Categoria getCategoriaAutonomo() {
return categoriaAutonomo;
}
//Setters
public void setTipoPerfil(String tipoPerfil) {
this.tipoPerfil = tipoPerfil;
}
public void setNome(String nome) {
this.nome = nome;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public void setEmail(String email) {
this.email = email;
}
public void setSenha(String senha) {
this.senha = senha;
}
public void setTelefone(Integer telefone) {
this.telefone = telefone;
}
public void setAvaliacao(Double avaliacao) {
this.avaliacao = avaliacao;
}
public void setEndereco(Endereco endereco) {
this.endereco = endereco;
}
public void setCategoriaAutonomo(Categoria categoriaAutonomo) {
this.categoriaAutonomo = categoriaAutonomo;
}
}
//--- 存储库
@Autowired
private PerfilService perfilService = new PerfilService();
//--- USUARIOS
@GetMapping("/usuarios")
public ResponseEntity getUsuario(){
return perfilService.getPerfisRepository();
}
@PostMapping("/cadastrar-usuario")
public ResponseEntity cadastrarUsuario(@RequestBody @Valid Perfil novoPerfil){
return perfilService.cadastrarUsuario(novoPerfil);
}
@PutMapping("/usuarios/{id}")
public ResponseEntity alterarUsuario(@RequestBody @Valid Perfil usuarioAlterado, @PathVariable int id){
return perfilService.alterarPerfil(usuarioAlterado,id);
}
这就是我更改 perfil 对象的方法
public ResponseEntity alterarPerfil(Perfil perfilAlterado, int id){
perfisRepository.findById(id).map(perfil -> {
perfil.setNome(perfilAlterado.getNome());
perfil.setEmail(perfilAlterado.getEmail());
perfil.setTelefone(perfilAlterado.getTelefone());
return ResponseEntity.ok().build();
});
return ResponseEntity.badRequest().body("Alteração inválida!");
}
我像这样在邮递员中发送我的放置请求:
{
"id": 1,
"nome": "Beatriz Barros",
"cpf": "103.725.810-05",
"email": "bia@hotmail.com",
"senha": "121520",
"telefone": 1134577777,
"avaliacao": 4.9,
"endereco": {
"id": 1,
"cep": "03311111",
"bairro": "Vila Poeira",
"logradouro": "RuaFlor",
"numeroLogradouro": 7,
"complemento": null,
"uf": "sp",
"cidade": "SaoPaulo"
},
"tipoPerfil": "autonomo",
"categoriaAutonomo": {
"id": 1,
"nome": "Jardineiro",
"descricao": null
},
"precoAutonomo": 0.0
}
但它总是返回状态 400 错误请求! 我已经尝试只发送我想更改的字段(姓名、电子邮件和电话),但也没有用
我也尝试使用和不使用 ID 发送,但没有任何工作
如何发送正确的请求?
【问题讨论】:
标签: java spring-boot validation request put