3
I wonder if my reasoning is correct regarding the use of a DTO
Following this logic, I am correct in using a DTO?
@Controller
@RequestMapping("/")
public class CadastroController
{
private final CadastroRepository cadastroRepository;
@Autowired
public CadastroController(CadastroRepository cadastroRepository)
{
this.cadastroRepository = cadastroRepository;
}
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public void save(final CadastroDTO cadastroDTO)
{
CadastroEntity cadastroEntity = new CadastroEntity();
cadastroEntity.setNome(cadastroDTO.getNome());
cadastroRepository.save(cadastroEntity);
}
}
public class CadastroDTO
{
@Getter
@Setter
private String nome;
}
@Entity
@Table(name = "cadastro")
public class CadastroEntity implements Serializable
{
@Id
@GeneratedValue
@Getter
private Long id;
@Getter
@Setter
private String nome;
}
If yes, I should use DTO only to store the information without the risk of changing the information in the database as it would with an entity?
You’re using Ombok, right?
– Victor Stafusa
Yes, I use Ombok yes at Getter Setter
– Joao Alberto