@Getter
@Setter
public class MemberForm {
@NotEmpty(message = "회원 이름은 필수 입니다")
private String name;
private String city;
private String street;
private String zipcode;
}
@PostMapping("/members/new")
public String create(@Valid MemberForm form, BindingResult result){
if(result.hasErrors()){
return "members/createMemberForm";
}
Address address = new Address(form.getCity(), form.getStreet(), form.getZipcode());
Member member = new Member();
member.setName(form.getName());
member.setAddress(address);
memberService.join(member);
return "redirect:/";
}
@GetMapping("/members")
public String list(Model model){
// member entity를 그대로 뿌리기 보다는 dto로 변환해서 화면에 꼭 필요한 데이터만 가지고 출력하는 방법 추천
List<Member> members = memberService.findMembers();
model.addAttribute("members",members);
}
- API 설계 시 Entity 그대로 전달하면 안된다!! 이유불문 entity 넘기면 안됨!! 외부반환금지!
@Entity
public class Member {
@Id @GeneratedValue
@Column(name="member_id")
private Long id;
private String name;
@Embedded
private Address address;
@OneToMany(mappedBy = "member")
private List<Order> orders = new ArrayList<>();
}
@Entity
public class Member {
@Id @GeneratedValue
@Column(name="member_id")
private Long id;
private String name;
private String password;
@Embedded
private Address address;
@OneToMany(mappedBy = "member")
private List<Order> orders = new ArrayList<>();
}