├── .gitignore
├── README.md
├── pom.xml
└── src
├── main
├── java
│ └── com
│ │ └── example
│ │ └── restfulwebservice
│ │ ├── RestfulWebServiceApplication.java
│ │ ├── config
│ │ ├── SecurityConfig.java
│ │ └── SwaggerConfig.java
│ │ ├── exception
│ │ ├── CustomizedResponseEntityExceptionHandler.java
│ │ └── ExceptionResponse.java
│ │ ├── helloworld
│ │ ├── HelloWorldBean.java
│ │ └── HelloWorldController.java
│ │ └── user
│ │ ├── AdminUserController.java
│ │ ├── Post.java
│ │ ├── PostRepository.java
│ │ ├── User.java
│ │ ├── UserController.java
│ │ ├── UserDaoService.java
│ │ ├── UserJpaController.java
│ │ ├── UserNotFoundException.java
│ │ ├── UserRepository.java
│ │ └── UserV2.java
└── resources
│ ├── application.yml
│ ├── data.sql
│ ├── messages.properties
│ ├── messages_en.properties
│ └── messages_fr.properties
└── test
└── java
└── com
└── example
└── restfulwebservice
└── RestfulWebServiceApplicationTests.java
/.gitignore:
--------------------------------------------------------------------------------
1 | HELP.md
2 | target/
3 | !.mvn/wrapper/maven-wrapper.jar
4 | !**/src/main/**
5 | !**/src/test/**
6 |
7 | ### STS ###
8 | .apt_generated
9 | .classpath
10 | .factorypath
11 | .project
12 | .settings
13 | .springBeans
14 | .sts4-cache
15 |
16 | ### IntelliJ IDEA ###
17 | .idea
18 | *.iws
19 | *.iml
20 | *.ipr
21 |
22 | ### NetBeans ###
23 | /nbproject/private/
24 | /nbbuild/
25 | /dist/
26 | /nbdist/
27 | /.nb-gradle/
28 | build/
29 |
30 | ### VS Code ###
31 | .vscode/
32 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Spring Boot를 이용한 RESTful Web Service 개발
2 |
3 | 1. 사용자 관리 API
4 | * 전체 사용자 목록 조회: GET HTTP Method, http://localhost:8088/users
5 | * 개별 사용자 조회: GET HTTP Method, http://localhost:8088/users/{id}
6 | * 사용자 삭제: DELETE HTTP Method, http://localhost:8088/users/{id}
7 | * 사용자 정보 수정: PUT HTTP Method, http://localhost:8088/users/{id}
8 |
9 | 2. 게시물 관리 API
10 | * 전체 게시물 목록 조회: GET HTTP Method, http://localhost:8088/users/{id}/posts
11 | * 게시물 삭제: DELETE HTTP Method, http://localhost:8088/users/{id}/posts/{post_id}
12 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.1.13.RELEASE
9 |
10 |
11 | com.example
12 | restful-web-service
13 | 0.0.1-SNAPSHOT
14 | restful-web-service
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-data-jpa
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-web
29 |
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-devtools
34 | runtime
35 | true
36 |
37 |
38 | com.h2database
39 | h2
40 | runtime
41 |
42 |
43 | org.projectlombok
44 | lombok
45 | true
46 |
47 |
48 | org.springframework.boot
49 | spring-boot-starter-test
50 | test
51 |
52 |
53 | org.springframework.boot
54 | spring-boot-starter-hateoas
55 |
56 |
57 | org.springframework.boot
58 | spring-boot-starter-actuator
59 |
60 |
61 | org.springframework.data
62 | spring-data-rest-hal-browser
63 |
64 |
65 | org.springframework.boot
66 | spring-boot-starter-security
67 |
68 |
69 |
70 | org.springframework.boot
71 | spring-boot-starter-data-jpa
72 |
73 |
74 | com.h2database
75 | h2
76 | runtime
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 | io.springfox
86 | springfox-swagger2
87 | 2.9.2
88 |
89 |
90 | io.springfox
91 | springfox-swagger-ui
92 | 2.9.2
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 | org.springframework.boot
101 | spring-boot-maven-plugin
102 |
103 |
104 |
105 |
106 |
107 |
--------------------------------------------------------------------------------
/src/main/java/com/example/restfulwebservice/RestfulWebServiceApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.restfulwebservice;
2 |
3 | import org.hibernate.Session;
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 | import org.springframework.context.annotation.Bean;
7 | import org.springframework.web.servlet.LocaleResolver;
8 | import org.springframework.web.servlet.i18n.SessionLocaleResolver;
9 |
10 | import java.util.Locale;
11 |
12 | @SpringBootApplication
13 | public class RestfulWebServiceApplication {
14 |
15 | public static void main(String[] args) {
16 | SpringApplication.run(RestfulWebServiceApplication.class, args);
17 | }
18 |
19 | @Bean
20 | public LocaleResolver localeResolver() {
21 | SessionLocaleResolver localeResolver = new SessionLocaleResolver();
22 | localeResolver.setDefaultLocale(Locale.KOREA);
23 | return localeResolver;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/example/restfulwebservice/config/SecurityConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.restfulwebservice.config;
2 |
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.context.annotation.Configuration;
5 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
6 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
7 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
8 |
9 | @Configuration
10 | public class SecurityConfig extends WebSecurityConfigurerAdapter {
11 |
12 | @Override
13 | protected void configure(HttpSecurity http) throws Exception {
14 | http.authorizeRequests().antMatchers("/h2-console/**").permitAll();
15 | http.csrf().disable();
16 | http.headers().frameOptions().disable();
17 | }
18 |
19 | @Autowired
20 | public void configureGlobal(AuthenticationManagerBuilder auth)
21 | throws Exception {
22 | auth.inMemoryAuthentication()
23 | .withUser("kenneth")
24 | .password("{noop}test1234")
25 | .roles("USER");
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/com/example/restfulwebservice/config/SwaggerConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.restfulwebservice.config;
2 |
3 | import org.springframework.context.annotation.Bean;
4 | import org.springframework.context.annotation.Configuration;
5 | import springfox.documentation.service.ApiInfo;
6 | import springfox.documentation.service.Contact;
7 | import springfox.documentation.spi.DocumentationType;
8 | import springfox.documentation.spring.web.plugins.Docket;
9 | import springfox.documentation.swagger2.annotations.EnableSwagger2;
10 |
11 | import java.util.ArrayList;
12 | import java.util.Arrays;
13 | import java.util.HashSet;
14 | import java.util.Set;
15 |
16 | @Configuration
17 | @EnableSwagger2
18 | public class SwaggerConfig {
19 | private static final Contact DEFAULT_CONTACT = new Contact("Kenneth Lee",
20 | "http://www.joneconsulting.co.kr", "edowon@joneconsulting.co.kr");
21 |
22 | private static final ApiInfo DEFAULT_API_INFO = new ApiInfo("Awesome API Title",
23 | "My User management REST API service", "1.0", "urn:tos",
24 | DEFAULT_CONTACT, "Apache 2.0",
25 | "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList<>());
26 |
27 | private static final Set DEFAULT_PRODUCES_AND_CONSUMES = new HashSet<>(
28 | Arrays.asList("application/json", "application/xml"));
29 |
30 | @Bean
31 | public Docket api() {
32 | return new Docket(DocumentationType.SWAGGER_2)
33 | .apiInfo(DEFAULT_API_INFO)
34 | .produces(DEFAULT_PRODUCES_AND_CONSUMES)
35 | .consumes(DEFAULT_PRODUCES_AND_CONSUMES);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/com/example/restfulwebservice/exception/CustomizedResponseEntityExceptionHandler.java:
--------------------------------------------------------------------------------
1 | package com.example.restfulwebservice.exception;
2 |
3 | import com.example.restfulwebservice.user.UserNotFoundException;
4 | import org.springframework.http.HttpHeaders;
5 | import org.springframework.http.HttpStatus;
6 | import org.springframework.http.ResponseEntity;
7 | import org.springframework.web.bind.MethodArgumentNotValidException;
8 | import org.springframework.web.bind.annotation.ControllerAdvice;
9 | import org.springframework.web.bind.annotation.ExceptionHandler;
10 | import org.springframework.web.bind.annotation.RestController;
11 | import org.springframework.web.context.request.WebRequest;
12 | import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
13 |
14 | import java.util.Date;
15 |
16 | @RestController
17 | @ControllerAdvice
18 | public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
19 |
20 | @ExceptionHandler(Exception.class)
21 | public final ResponseEntity