-
Notifications
You must be signed in to change notification settings - Fork 0
2차 세미나 기본과제 #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
2차 세미나 기본과제 #5
Conversation
import java.util.ArrayList; | ||
@SpringBootApplication |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
9번째 줄과 10번째 줄 띄어쓰기 해주시면 좋을 것 같아요
public static ArrayList<User> userList; | ||
public static ArrayList<Article> articleList; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
static과 non-static의 차이는 무엇일까요?
이걸 static으로 두면 어떤 일이 발생할까요?
@PostMapping("/article") | ||
public String register(@RequestBody final RegisterArticleRequestDto request) { | ||
System.out.println(request.getTitle()); | ||
System.out.println(request.getDetail()); | ||
|
||
// 서비스 계층에 유저를 등록하는 메서드를 호출 | ||
|
||
return "게시물 등록이 완료됐습니다."; | ||
} | ||
|
||
@GetMapping("/article/{articleId}") | ||
public String getOne(@PathVariable final Long articleId) { | ||
System.out.println("요청 게시물 아이디: " + articleId); | ||
|
||
// 서비스 계층에서 유저 아이디로 유저를 찾는 메서드 호출 | ||
|
||
return "게시물 조회 성공"; | ||
} | ||
|
||
@GetMapping("/article/search") | ||
public String search(@RequestParam final String title) { | ||
System.out.println("게시물 제목 검색 인자: " + title); | ||
|
||
// 서비스 계층에서 유저 닉네임으로 유저를 찾는 메서드 호출 | ||
|
||
return "게시물 검색 성공"; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
컨트롤러의 어노테이션으로 @RequestMapping("/article")을 붙여주면 조금 더 편할 거에요! 검색해보세요.
public class RegisterArticleRequestDto { | ||
|
||
private String title; | ||
|
||
private String detail; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
필드를 불변으로 해주면 더 좋을 것 같아요! 바뀌면 안되는 값들이잖아용:)
@RequiredArgsConstructor | ||
public class UserController { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
요긴 RequiredArgsConstructor를 쓰셧네요
AllArgsConstructor와 RequiredArgsConstructor의 차이는 무엇일까요?
@RestController | ||
@RequiredArgsConstructor | ||
public class UserController { | ||
private final UserService userService; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Autowired를 하지 않아도 자동으로 스프링 빈으로 주입이 되는데 왜그럴까요?
public class UserController { | ||
private final UserService userService; | ||
|
||
@PostMapping("/user") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
마찬가지로 컨트롤러 어노테이션으로 @RequiredMapping으로 해주시면 좋을 것 같아요!
"contact: " + this.contact + "\n" + | ||
"age: " + this.age; | ||
} | ||
} No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
마지막줄 한줄 띄워줘여 ㅋㅋ
public void setId(Long id) { | ||
this.id = id; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
도메인은 굉장히 중요한 객체인데요. 변하는 것을 최대한 방어해야한다고 생각해요.
세터를 쓰는 것보다 생성자를 두 개 만들어서 설정해보면 어떨까요?
public class User { | ||
|
||
private Long id; | ||
|
||
private String gender; | ||
|
||
private String name; | ||
|
||
private String address; | ||
|
||
private String contact; | ||
|
||
private int age; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
마찬가지로 도메인 관련 이야기인데요. final 키워드를 붙여보시면 조금 더 객체지향적으로 변할 겁니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
자바윤서 ㄷ ㄷ
스위프트 없이 행복해?
|
||
// 데이터베이스에 저장 | ||
articleList.add(newArticle); | ||
newArticle.setId((long) articleList.size()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
타입케스팅의 다른 방법도 존재합니다! toLong()
처럼..?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이렇게 강제 케스팅을 하지 않는 이유는, toLong()
코드를 뜯어보면 아시겠지만 예외처리를 해줍니다:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
고생하셨슴돠
처음인거 맞나요? 구조 미쳤네요
// 서비스 계층에서 유저 닉네임으로 유저를 찾는 메서드 호출 | ||
|
||
return "게시물 검색 성공"; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
검색하려는 게시물이 없을 때에 대한 예외처리도 해주시면 더 보기 좋을 것 같습니다-!!👍
제가 발견한 부분은 다른 분들께서 다 작성해주셔서😂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
고생하셨습니다!
✅ 과제