Skip to content

Commit

Permalink
Merge pull request #5 from hariprasath-r/refactor-restfully
Browse files Browse the repository at this point in the history
Refactor restfully
  • Loading branch information
hariprasath-r authored Apr 27, 2020
2 parents 4576b74 + 69c7071 commit 2064dd9
Show file tree
Hide file tree
Showing 24 changed files with 507 additions and 184 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
/nbdist/
/.nb-gradle/
/bin/
/.idea/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# springboot-course-data-api

Simple POC using Spring Boot to create a Course API.
A simple POC using Spring Boot to create a Course API.
----------------------------------------------------

Spring Boot Dependencies Used:
Expand Down
13 changes: 0 additions & 13 deletions derby.log

This file was deleted.

25 changes: 23 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,35 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>

<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/in/hp/java/CourseDataApiApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,33 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;

import java.util.Locale;

@SpringBootApplication
public class CourseDataApiApplication {

public static void main(String[] args) {
SpringApplication.run(CourseDataApiApplication.class, args);
}

/**
* In this bean we are configuring default locale and returning
* If a locale is not being sent in request this default will be used
*
* Since we are fetching from header here
* replacing SessionLocaleResolver to AcceptHeaderLocaleResolver
*
* @return AcceptHeaderLocaleResolver
*/
@Bean
public LocaleResolver buildLocale() {
AcceptHeaderLocaleResolver acceptHeaderLocaleResolver = new AcceptHeaderLocaleResolver();
acceptHeaderLocaleResolver.setDefaultLocale(Locale.US);
return acceptHeaderLocaleResolver;
}

}
60 changes: 60 additions & 0 deletions src/main/java/in/hp/java/SwaggerConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package in.hp.java;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

private static final Contact DEFAULT_CONTACT = new Contact(
"Hariprasath",
"https://www.github.com/hariprasath-r",
"");

private static final ApiInfo DEFAULT_API_INFO = new ApiInfo(
"Course Data API",
"Provides Creation and Maintaining Courses",
"1.0",
"urn:tos",
DEFAULT_CONTACT,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<VendorExtension>());

private static final Set<String> DEFAULT_PRODUCES_CONSUMES =
new HashSet<>(Arrays.asList("application/json", "application.xml"));

/**
* Configuring and building Docket Object
* select() - allows to build the Docket with ApiSelectBuilder
* apis() - allows to configure the basePackage from which the models will be read
* paths() - allows to configure URI/resource path
* build() - returns a Docket object
*
* @return
*/
@Bean
public Docket configureDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("in.hp.java"))
// .paths(PathSelectors.ant("/*"))
.build() // building Docker Object
.apiInfo(DEFAULT_API_INFO)
.produces(DEFAULT_PRODUCES_CONSUMES)
.consumes(DEFAULT_PRODUCES_CONSUMES);
}
}
36 changes: 36 additions & 0 deletions src/main/java/in/hp/java/boot/InternationalizationDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package in.hp.java.boot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController()
public class InternationalizationDemo {

/**
* Autowiring message source to read the messages against passed input
*/
@Autowired
ResourceBundleMessageSource messageSource;

@GetMapping("/greeting")
public String greet() {
return "Good Morning";
}

/**
* Injecting the locale from request from the header
* Accept-Language - where locale is passed
* required - false, since we already have default locale configured
*
* Instead of accepting from header everywhere, we can use LocaleContextHolder to retrieve the value
*
* @return
*/
@GetMapping("/i18n")
public String internationalGreeting() {
return messageSource.getMessage("good.morning.message", null, LocaleContextHolder.getLocale());
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
package in.hp.java.boot.controller.course;
package in.hp.java.boot.course;

import com.fasterxml.jackson.annotation.JsonIgnore;
import in.hp.java.boot.topic.Topic;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

import in.hp.java.boot.controller.topic.Topic;

import javax.validation.constraints.Size;

/**
* @JsonIgnoreProperties - used to ignore certain properties during Json conversion
* @JsonIgnore - recommended to use
* @JsonFilter - Indicated that the bean can be filtered
*/
//@JsonIgnoreProperties(value = {"topic"})
//@JsonFilter("SomeBeanFilter")
@ApiModel("Course Details")
@Entity
public class Course {

@Id
@Size(min = 2, message = "Id should be of 2 chars minimum")
@ApiModelProperty(notes = "Id should be of 2 chars minimum")
private String id;

@Size(min = 4, message = "Name should be of 4 chars minimum")
@ApiModelProperty(notes = "Name should be of 4 chars minimum")
private String name;
private String description;

/*
* Adding JPA @ManyToOne annotation to let Spring JPA know it needs to establish
* a Foreign Key relationship
*
* FetchType can be made LAZY
*
* @JsonIgnore is used to ignore this field in response
*/
@ManyToOne(fetch = FetchType.EAGER)
@JsonIgnore
private Topic topic;

public Course() {
Expand Down Expand Up @@ -76,3 +95,4 @@ public void setTopic(Topic topic) {
}

}

Loading

0 comments on commit 2064dd9

Please sign in to comment.