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 @RestController is used instead of @Controller, it returns the body of the response.
+ You can use ResponseEntity when using @Responsebody or @RestController. ResponseEntity is same as the HttpResponseServlet. In this case, the RestController must return ResponseEntity.
How does Spring's http req/res works?
1. Let's take a look at a HTTP Request's structure.
HTTP Request consists of Line, Header, and Body.
1. Line
Line is the key of the request. It consists of Method, URI, and Protocol.
1. Method: Method is a type of a request. It has GET, POST, PUT, and DELETE. It defines which action the server should do.
2. URI: URI is a something closer to the directory. It points to the path that the client wants the server to look into.
3. Protocol: Protocol is a protocol that the communication is following. It is mostly HTTP.
2. Header
Header is the metadata of the request. It can is key-value pair, and it consists of data like1. host:server's domain and port2. Connection: whether if the connection would be alive after the transaction3.Accept:the data type that the server should return.
3. BodyBody is a extra data that the client sends. It can be anything that the client wants.
It consists like this, but it many cases, request puts many different data into the URI; especially in GET request because it does have an empty body. It is called 'path variable' or 'request parameter'. One good example of request parameter is query string.
Thus, there are 3 frequently used types of HTTP Requests.
The most basic way to handle request and response is using HttpServletRequest and HttpServletResponse objects as parameters.
HttpServletRequest carries all the data of the given request and has many various methods like getMethod(), getHeader(), getDateHeader() and so on.
HttpServletResponse has similar methods, but its main is setters. It has methods like setContentType() or you can add cookies like
cookie.setMaxAge(600); //600초However, there are lots of parameters or annotations that can be used to control the HTTP Request in Spring.
1. @PathVariable
Path Variable literally means that the variable is included in the path. When using a path variable, URL's path looks like google.com/search/hello. In this case, it would be 'searching' the word 'hello'. To use the annotation @PathVariable, the request mapper should have been used in advance.
For example,
@GetMapping("/id/{id}")
public Integer returnId(@PathVariable("id") Int id)
{
return id;
}
You can use "id" as a parameter if there's same-named parameter in the @GetMapping. If the parameter's name(Int id) is same with the @GetMapping, you can omit the "id"; you can just the @PathVariable)
2. @RequestParam
Request Parameter can be used instead of request.getParameter("id"). This is used to get Query Parameters in URL or HTML Form. It has two options: required, defaultValue. required determines if the parameter is essential, and defaultValue is the default value when the parameter isnt given. If required = false and no default value, the parameter will be passed as NULL.
Request Parameter can be loaded in the request in 2 different ways. First is to load it in the URL when using GET Method, and the Second is to load it in the Body when using Post Method with HTML FORM.
+RequestParam is good, but using DTO is recommended for eliminiating dependencies. When DTO is used as a parameter, the Spring compares the parameters given in the request and the DTO's attribute. When they are identical, the Spring maps the request's data into the DTO. For example,
/GetMapping("/ab")
public String abcd(AbcdDTO abcdDto)
{
/.../
}
When the request into the path /ab has query parameters 'id' and 'name' and the AbcdDTO also has attributes of 'id' and 'name, the Spring maps the the values of 'id' and 'name' of the request into the parameter abcdDto.
As you can see, Dto you can eliminiate the dependency using DTO. When you use '@RequestParam Int id', you should modify the code of it. But when you use DTO, you just have to modify the code of the DTO. Thus, when the there are only few, and simple(int or String) parameters, and the parameters are hardly likely to change, you can use the @RequestParam. If not, you must use the DTO.
Then, let's take a look at HTTP response's structure.
HTTP Response consists of Line, Header, and Body just like the Request, the has some differences in its contents.
1. Line
Line of the response contains the Status of the response.
1. HTTP version: HTTP version is literally the version of the HTTP comm. It usually looks like HTTP/2.0
2. Status Code: It is 3-digit code that represents the status. It is like 200, 404, 500.
3. Status Text: It is a short text that represents the status. It looks like 'Not Found'.
2. Header
Header is the metadata of the response. It can is key-value pair, but it has some different fields from that of the request; for example, it has 'server' instead of the 'User-agent'
3. BodyBody is a extra data that the server sends. It can be anything that the server wants.
댓글
댓글 쓰기