8월, 2024의 게시물 표시

BE Study 13th

Receiving JSON body of the HTTP request 1. @RequestBody Using RequestBody, you can receive the body of the request as a JSON: Content-Type: application/json Without RequestBody, the parameter will be mapped via @RequestParam(@RequestParam is omitted). @PrePersist Problem Two ways of initializing the field of the entity. 1. public Comment( @NonNull String postId, @NonNull String authorId, String content ) { this.postId = postId; this.authorId = authorId; this.content = content; this.createdDate = LocalDate.now(); } 2. public Comment( @NonNull String postId, @NonNull String authorId, String content ) { this.postId = postId; this.authorId = authorId; this.content = content; } @PrePersist public void prePersist() { createdDate = LocalDate.now(); } The latter(2) is recommended, in terms of entity lifecycle and persistence context and DB's consistence. 3...

BE Study 12th

이미지
Why use VO? there are some objects that are suitable for VO. For example, there is an 'application form'. If there are two different application form that contains exactly the same content. Should we accept both? It would be ridiculous. We should regard it a duplication. JPA JPA is a essential library that enables spring to manipulate the DB. There's a critical concept called "entity manager". Entity manager is an object offerred by JPA that enables the management of entities. Dirty Checking You don't have to actually em.persist. or em.update. When em is flush()ed, it makes an snapshot of the entities that are enrolled in the persistence context; thus the snapshot would be same with the current state of the DB. The next time when ex is flush()ed, the em compares the snapshot and the current state of the entities. Then it creates an UPDATE query which would make the snapshot same with the current state. Then it pushes the UPDATE query into the store query. 1. d...

BE Study 11th

Dependency Injection. Dependency Injection is that, the Dependency is injected in the runtime, not the compile time. For example, class HelloFactory() {     private Helloer helloer = new HelloerImpl();     public HelloFactory() { ... }     public sayHello()     {          helloer.sayHello();     } } The above looks descent(delegation pattern). However, the problem is that the class HelloFactory is depending on the class HelloerImpl() in the compile time.  class HelloFactory() {     private final Helloer helloer;     public HelloFactory(Helloer helloer)     {          this.helloer = helloer;     }     public sayHello()     {          helloer.sayHello();     } } The above code is much better, because you can inject the dependency(of helloer) in the runtime like helloFactory = HelloFactory(new...

Web Service Basic Architecture

헤더 정보 Request Method: 요청 메소드(GET, POST, PUT, DELETE 등) Request URL: 요청 URL Host: 요청한 서버의 도메인명 또는 IP 주소 User-Agent: 클라이언트의 브라우저 정보 Accept: 클라이언트가 받아들일 수 있는 MIME 타입 Content-Type: 요청 바디의 MIME 타입 Authorization: 인증 토큰 정보 등

BE Study 10th

Spring Setting 1. application.properties(or application.yml) the file is for setting 'common application properties'. Then, what would be the 'common application properties'? It is for setting the core settings for the 'overall project' including, DB connection, logger, server connection, etc. 2. pom.xml the file for managing dependencies for building the project. 3. ViewResolver What receive's controller's return is called 'ViewResolver'. There are several 'default' ViewResolvers in  Spring. The below ViewResolvers work in the below order: chain of reponsibility pattern. 1. ContentNegotiatingViewResolver: uses URL 2. BeanNameViewResolver: uses name of the Beans 3. ViewResolverComposite 4. InternalResourceViewResolver: JSP if you add dependencies like 'thymeleaf', the ViewResolver that gives a view of HTML file is available. +@Controller returns string or view so that the ViewResolver, but when used with @Responsebody or @RestCont...

BE Study 9th

@Controller public class CreatePostController { @Controller and CreatePostController Definition     private static final Logger log = LoggerFactory.getLogger(CreatePostController.class); Logger for logging     private static final String MODEL_ATTRIBUTE_POST = "post";     private final CreatePostHandler createPostHandler; CreatePostHandler may be a class that does some kind of operation for the controller(delegation pattern?)     public CreatePostController(final CreatePostHandler createPostHandler) {         this.createPostHandler = createPostHandler;     } consturctor. Gives createPostHandler.     @GetMapping("/posts/new")     @AnyAuthenticatedUser     public String newPostForm(Model model) {         model.addAttribute(MODEL_ATTRIBUTE_POST, new CreatePostRequest("", "", "", null, null));         return "posts/add-post";     } adds attrib...