Skip to content

Commit 2064dd9

Browse files
Merge pull request #5 from hariprasath-r/refactor-restfully
Refactor restfully
2 parents 4576b74 + 69c7071 commit 2064dd9

24 files changed

Lines changed: 507 additions & 184 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
/nbdist/
2525
/.nb-gradle/
2626
/bin/
27+
/.idea/

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# springboot-course-data-api
22

3-
Simple POC using Spring Boot to create a Course API.
3+
A simple POC using Spring Boot to create a Course API.
44
----------------------------------------------------
55

66
Spring Boot Dependencies Used:

derby.log

Lines changed: 0 additions & 13 deletions
This file was deleted.

pom.xml

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,35 @@
2828
<groupId>org.springframework.boot</groupId>
2929
<artifactId>spring-boot-starter-web</artifactId>
3030
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-security</artifactId>
34+
</dependency>
3135
<dependency>
3236
<groupId>org.springframework.boot</groupId>
3337
<artifactId>spring-boot-starter-actuator</artifactId>
3438
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-devtools</artifactId>
42+
<scope>runtime</scope>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>io.springfox</groupId>
47+
<artifactId>springfox-swagger2</artifactId>
48+
<version>2.9.2</version>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>io.springfox</groupId>
53+
<artifactId>springfox-swagger-ui</artifactId>
54+
<version>2.9.2</version>
55+
</dependency>
3556

3657
<dependency>
37-
<groupId>org.apache.derby</groupId>
38-
<artifactId>derby</artifactId>
58+
<groupId>com.h2database</groupId>
59+
<artifactId>h2</artifactId>
3960
<scope>runtime</scope>
4061
</dependency>
4162
<dependency>

src/main/java/in/hp/java/CourseDataApiApplication.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,33 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.web.servlet.LocaleResolver;
7+
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
8+
9+
import java.util.Locale;
510

611
@SpringBootApplication
712
public class CourseDataApiApplication {
813

914
public static void main(String[] args) {
1015
SpringApplication.run(CourseDataApiApplication.class, args);
1116
}
17+
18+
/**
19+
* In this bean we are configuring default locale and returning
20+
* If a locale is not being sent in request this default will be used
21+
*
22+
* Since we are fetching from header here
23+
* replacing SessionLocaleResolver to AcceptHeaderLocaleResolver
24+
*
25+
* @return AcceptHeaderLocaleResolver
26+
*/
27+
@Bean
28+
public LocaleResolver buildLocale() {
29+
AcceptHeaderLocaleResolver acceptHeaderLocaleResolver = new AcceptHeaderLocaleResolver();
30+
acceptHeaderLocaleResolver.setDefaultLocale(Locale.US);
31+
return acceptHeaderLocaleResolver;
32+
}
33+
1234
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package in.hp.java;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import springfox.documentation.builders.RequestHandlerSelectors;
6+
import springfox.documentation.service.ApiInfo;
7+
import springfox.documentation.service.Contact;
8+
import springfox.documentation.service.VendorExtension;
9+
import springfox.documentation.spi.DocumentationType;
10+
import springfox.documentation.spring.web.plugins.Docket;
11+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
12+
13+
import java.util.ArrayList;
14+
import java.util.Arrays;
15+
import java.util.HashSet;
16+
import java.util.Set;
17+
18+
@Configuration
19+
@EnableSwagger2
20+
public class SwaggerConfig {
21+
22+
private static final Contact DEFAULT_CONTACT = new Contact(
23+
"Hariprasath",
24+
"https://www.github.com/hariprasath-r",
25+
"");
26+
27+
private static final ApiInfo DEFAULT_API_INFO = new ApiInfo(
28+
"Course Data API",
29+
"Provides Creation and Maintaining Courses",
30+
"1.0",
31+
"urn:tos",
32+
DEFAULT_CONTACT,
33+
"Apache 2.0",
34+
"http://www.apache.org/licenses/LICENSE-2.0",
35+
new ArrayList<VendorExtension>());
36+
37+
private static final Set<String> DEFAULT_PRODUCES_CONSUMES =
38+
new HashSet<>(Arrays.asList("application/json", "application.xml"));
39+
40+
/**
41+
* Configuring and building Docket Object
42+
* select() - allows to build the Docket with ApiSelectBuilder
43+
* apis() - allows to configure the basePackage from which the models will be read
44+
* paths() - allows to configure URI/resource path
45+
* build() - returns a Docket object
46+
*
47+
* @return
48+
*/
49+
@Bean
50+
public Docket configureDocket() {
51+
return new Docket(DocumentationType.SWAGGER_2)
52+
.select()
53+
.apis(RequestHandlerSelectors.basePackage("in.hp.java"))
54+
// .paths(PathSelectors.ant("/*"))
55+
.build() // building Docker Object
56+
.apiInfo(DEFAULT_API_INFO)
57+
.produces(DEFAULT_PRODUCES_CONSUMES)
58+
.consumes(DEFAULT_PRODUCES_CONSUMES);
59+
}
60+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package in.hp.java.boot;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.i18n.LocaleContextHolder;
5+
import org.springframework.context.support.ResourceBundleMessageSource;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController()
10+
public class InternationalizationDemo {
11+
12+
/**
13+
* Autowiring message source to read the messages against passed input
14+
*/
15+
@Autowired
16+
ResourceBundleMessageSource messageSource;
17+
18+
@GetMapping("/greeting")
19+
public String greet() {
20+
return "Good Morning";
21+
}
22+
23+
/**
24+
* Injecting the locale from request from the header
25+
* Accept-Language - where locale is passed
26+
* required - false, since we already have default locale configured
27+
*
28+
* Instead of accepting from header everywhere, we can use LocaleContextHolder to retrieve the value
29+
*
30+
* @return
31+
*/
32+
@GetMapping("/i18n")
33+
public String internationalGreeting() {
34+
return messageSource.getMessage("good.morning.message", null, LocaleContextHolder.getLocale());
35+
}
36+
}

src/main/java/in/hp/java/boot/controller/course/CourseController.java

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/main/java/in/hp/java/boot/controller/topic/TopicController.java

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/main/java/in/hp/java/boot/controller/topic/TopicRepository.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)