diff --git a/spring-boot-bootstrap/pom.xml b/spring-boot-bootstrap/pom.xml index 03ce9b6906..ecc72c85f5 100644 --- a/spring-boot-bootstrap/pom.xml +++ b/spring-boot-bootstrap/pom.xml @@ -15,25 +15,6 @@ ../parent-boot-2 - - org.springframework.boot diff --git a/spring-boot-ops/README.MD b/spring-boot-ops/README.MD new file mode 100644 index 0000000000..e2a9a6ab59 --- /dev/null +++ b/spring-boot-ops/README.MD @@ -0,0 +1,7 @@ +### The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring + +### Relevant Articles: + +- [Intro to Spring Boot Starters](http://www.baeldung.com/spring-boot-starters) +- [A Custom Data Binder in Spring MVC](http://www.baeldung.com/spring-mvc-custom-data-binder) diff --git a/spring-boot-ops/pom.xml b/spring-boot-ops/pom.xml index 0d0ccc0ef2..760442d386 100644 --- a/spring-boot-ops/pom.xml +++ b/spring-boot-ops/pom.xml @@ -19,12 +19,36 @@ + + org.baeldung.demo.DemoApplication UTF-8 UTF-8 1.8 + 3.1.1 + 3.3.7-1 + + + org.springframework.boot spring-boot-starter-web @@ -41,6 +65,77 @@ spring-boot-starter-test test + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + org.springframework.boot + spring-boot-starter-mail + + + + com.graphql-java + graphql-spring-boot-starter + 3.6.0 + + + + org.springframework.boot + spring-boot-starter-actuator + + + + com.h2database + h2 + runtime + + + + javax.persistence + javax.persistence-api + 2.2 + + + + org.togglz + togglz-spring-boot-starter + 2.4.1.Final + + + + com.google.guava + guava + 18.0 + + + + org.subethamail + subethasmtp + 3.1.7 + test + + + + org.webjars + bootstrap + ${bootstrap.version} + + + + org.webjars + jquery + ${jquery.version} + + + + com.graphql-java + graphql-java-tools + 3.2.0 + + @@ -50,8 +145,59 @@ org.springframework.boot spring-boot-maven-plugin + + + org.springframework.boot + spring-boot-maven-plugin + 1.5.2.RELEASE + + + + repackage + + + org.baeldung.boot.Application + + + + + + + autoconfiguration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*LiveTest.java + **/*IntegrationTest.java + + + **/AutoconfigurationTest.java + + + + + + + json + + + + + + + diff --git a/spring-boot-ops/src/main/java/com/baeldung/graphql/Author.java b/spring-boot-ops/src/main/java/com/baeldung/graphql/Author.java new file mode 100644 index 0000000000..11e927c564 --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/graphql/Author.java @@ -0,0 +1,31 @@ +package com.baeldung.graphql; + +public class Author { + private String id; + private String name; + private String thumbnail; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getThumbnail() { + return thumbnail; + } + + public void setThumbnail(String thumbnail) { + this.thumbnail = thumbnail; + } +} diff --git a/spring-boot-ops/src/main/java/com/baeldung/graphql/AuthorDao.java b/spring-boot-ops/src/main/java/com/baeldung/graphql/AuthorDao.java new file mode 100644 index 0000000000..c799a558a7 --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/graphql/AuthorDao.java @@ -0,0 +1,16 @@ +package com.baeldung.graphql; + +import java.util.List; +import java.util.Optional; + +public class AuthorDao { + private List authors; + + public AuthorDao(List authors) { + this.authors = authors; + } + + public Optional getAuthor(String id) { + return authors.stream().filter(author -> id.equals(author.getId())).findFirst(); + } +} diff --git a/spring-boot-ops/src/main/java/com/baeldung/graphql/AuthorResolver.java b/spring-boot-ops/src/main/java/com/baeldung/graphql/AuthorResolver.java new file mode 100644 index 0000000000..982c6cebc1 --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/graphql/AuthorResolver.java @@ -0,0 +1,17 @@ +package com.baeldung.graphql; + +import java.util.List; + +import com.coxautodev.graphql.tools.GraphQLResolver; + +public class AuthorResolver implements GraphQLResolver { + private PostDao postDao; + + public AuthorResolver(PostDao postDao) { + this.postDao = postDao; + } + + public List getPosts(Author author) { + return postDao.getAuthorPosts(author.getId()); + } +} diff --git a/spring-boot-ops/src/main/java/com/baeldung/graphql/GraphqlConfiguration.java b/spring-boot-ops/src/main/java/com/baeldung/graphql/GraphqlConfiguration.java new file mode 100644 index 0000000000..c5ae8bd772 --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/graphql/GraphqlConfiguration.java @@ -0,0 +1,59 @@ +package com.baeldung.graphql; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class GraphqlConfiguration { + @Bean + public PostDao postDao() { + List posts = new ArrayList<>(); + for (int postId = 0; postId < 10; ++postId) { + for (int authorId = 0; authorId < 10; ++authorId) { + Post post = new Post(); + post.setId("Post" + authorId + postId); + post.setTitle("Post " + authorId + ":" + postId); + post.setText("Post " + postId + " + by author " + authorId); + post.setAuthorId("Author" + authorId); + posts.add(post); + } + } + return new PostDao(posts); + } + + @Bean + public AuthorDao authorDao() { + List authors = new ArrayList<>(); + for (int authorId = 0; authorId < 10; ++authorId) { + Author author = new Author(); + author.setId("Author" + authorId); + author.setName("Author " + authorId); + author.setThumbnail("http://example.com/authors/" + authorId); + authors.add(author); + } + return new AuthorDao(authors); + } + + @Bean + public PostResolver postResolver(AuthorDao authorDao) { + return new PostResolver(authorDao); + } + + @Bean + public AuthorResolver authorResolver(PostDao postDao) { + return new AuthorResolver(postDao); + } + + @Bean + public Query query(PostDao postDao) { + return new Query(postDao); + } + + @Bean + public Mutation mutation(PostDao postDao) { + return new Mutation(postDao); + } +} diff --git a/spring-boot-ops/src/main/java/com/baeldung/graphql/Mutation.java b/spring-boot-ops/src/main/java/com/baeldung/graphql/Mutation.java new file mode 100644 index 0000000000..0e16e3c8b7 --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/graphql/Mutation.java @@ -0,0 +1,25 @@ +package com.baeldung.graphql; + +import java.util.UUID; + +import com.coxautodev.graphql.tools.GraphQLMutationResolver; + +public class Mutation implements GraphQLMutationResolver { + private PostDao postDao; + + public Mutation(PostDao postDao) { + this.postDao = postDao; + } + + public Post writePost(String title, String text, String category, String author) { + Post post = new Post(); + post.setId(UUID.randomUUID().toString()); + post.setTitle(title); + post.setText(text); + post.setCategory(category); + post.setAuthorId(author); + postDao.savePost(post); + + return post; + } +} diff --git a/spring-boot-ops/src/main/java/com/baeldung/graphql/Post.java b/spring-boot-ops/src/main/java/com/baeldung/graphql/Post.java new file mode 100644 index 0000000000..14d3084841 --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/graphql/Post.java @@ -0,0 +1,49 @@ +package com.baeldung.graphql; + +public class Post { + private String id; + private String title; + private String text; + private String category; + private String authorId; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public String getAuthorId() { + return authorId; + } + + public void setAuthorId(String authorId) { + this.authorId = authorId; + } +} diff --git a/spring-boot-ops/src/main/java/com/baeldung/graphql/PostDao.java b/spring-boot-ops/src/main/java/com/baeldung/graphql/PostDao.java new file mode 100644 index 0000000000..0a755a7cf4 --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/graphql/PostDao.java @@ -0,0 +1,24 @@ +package com.baeldung.graphql; + +import java.util.List; +import java.util.stream.Collectors; + +public class PostDao { + private List posts; + + public PostDao(List posts) { + this.posts = posts; + } + + public List getRecentPosts(int count, int offset) { + return posts.stream().skip(offset).limit(count).collect(Collectors.toList()); + } + + public List getAuthorPosts(String author) { + return posts.stream().filter(post -> author.equals(post.getAuthorId())).collect(Collectors.toList()); + } + + public void savePost(Post post) { + posts.add(0, post); + } +} diff --git a/spring-boot-ops/src/main/java/com/baeldung/graphql/PostResolver.java b/spring-boot-ops/src/main/java/com/baeldung/graphql/PostResolver.java new file mode 100644 index 0000000000..dbfde330ea --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/graphql/PostResolver.java @@ -0,0 +1,17 @@ +package com.baeldung.graphql; + +import java.util.Optional; + +import com.coxautodev.graphql.tools.GraphQLResolver; + +public class PostResolver implements GraphQLResolver { + private AuthorDao authorDao; + + public PostResolver(AuthorDao authorDao) { + this.authorDao = authorDao; + } + + public Optional getAuthor(Post post) { + return authorDao.getAuthor(post.getAuthorId()); + } +} diff --git a/spring-boot-ops/src/main/java/com/baeldung/graphql/Query.java b/spring-boot-ops/src/main/java/com/baeldung/graphql/Query.java new file mode 100644 index 0000000000..7bb625798c --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/graphql/Query.java @@ -0,0 +1,17 @@ +package com.baeldung.graphql; + +import java.util.List; + +import com.coxautodev.graphql.tools.GraphQLQueryResolver; + +public class Query implements GraphQLQueryResolver { + private PostDao postDao; + + public Query(PostDao postDao) { + this.postDao = postDao; + } + + public List recentPosts(int count, int offset) { + return postDao.getRecentPosts(count, offset); + } +} diff --git a/spring-boot/src/main/java/com/baeldung/shutdown/Application.java b/spring-boot-ops/src/main/java/com/baeldung/shutdown/Application.java similarity index 100% rename from spring-boot/src/main/java/com/baeldung/shutdown/Application.java rename to spring-boot-ops/src/main/java/com/baeldung/shutdown/Application.java diff --git a/spring-boot/src/main/java/com/baeldung/shutdown/ShutdownConfig.java b/spring-boot-ops/src/main/java/com/baeldung/shutdown/ShutdownConfig.java similarity index 100% rename from spring-boot/src/main/java/com/baeldung/shutdown/ShutdownConfig.java rename to spring-boot-ops/src/main/java/com/baeldung/shutdown/ShutdownConfig.java diff --git a/spring-boot/src/main/java/com/baeldung/shutdown/TerminateBean.java b/spring-boot-ops/src/main/java/com/baeldung/shutdown/TerminateBean.java similarity index 100% rename from spring-boot/src/main/java/com/baeldung/shutdown/TerminateBean.java rename to spring-boot-ops/src/main/java/com/baeldung/shutdown/TerminateBean.java diff --git a/spring-boot/src/main/java/com/baeldung/shutdown/shutdown/ShutdownController.java b/spring-boot-ops/src/main/java/com/baeldung/shutdown/shutdown/ShutdownController.java similarity index 100% rename from spring-boot/src/main/java/com/baeldung/shutdown/shutdown/ShutdownController.java rename to spring-boot-ops/src/main/java/com/baeldung/shutdown/shutdown/ShutdownController.java diff --git a/spring-boot-ops/src/main/java/com/baeldung/toggle/Employee.java b/spring-boot-ops/src/main/java/com/baeldung/toggle/Employee.java new file mode 100644 index 0000000000..64a8b3ce5b --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/toggle/Employee.java @@ -0,0 +1,37 @@ +package com.baeldung.toggle; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class Employee { + + @Id + private long id; + private double salary; + + public Employee() { + } + + public Employee(long id, double salary) { + this.id = id; + this.salary = salary; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public double getSalary() { + return salary; + } + + public void setSalary(double salary) { + this.salary = salary; + } + +} diff --git a/spring-boot-ops/src/main/java/com/baeldung/toggle/EmployeeRepository.java b/spring-boot-ops/src/main/java/com/baeldung/toggle/EmployeeRepository.java new file mode 100644 index 0000000000..4e75fc6411 --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/toggle/EmployeeRepository.java @@ -0,0 +1,7 @@ +package com.baeldung.toggle; + +import org.springframework.data.repository.CrudRepository; + +public interface EmployeeRepository extends CrudRepository { + +} diff --git a/spring-boot-ops/src/main/java/com/baeldung/toggle/FeatureAssociation.java b/spring-boot-ops/src/main/java/com/baeldung/toggle/FeatureAssociation.java new file mode 100644 index 0000000000..4578b8498e --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/toggle/FeatureAssociation.java @@ -0,0 +1,12 @@ +package com.baeldung.toggle; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.METHOD, ElementType.TYPE }) +public @interface FeatureAssociation { + MyFeatures value(); +} diff --git a/spring-boot-ops/src/main/java/com/baeldung/toggle/FeaturesAspect.java b/spring-boot-ops/src/main/java/com/baeldung/toggle/FeaturesAspect.java new file mode 100644 index 0000000000..04c6305780 --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/toggle/FeaturesAspect.java @@ -0,0 +1,26 @@ +package com.baeldung.toggle; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.springframework.stereotype.Component; + +@Aspect +@Component +public class FeaturesAspect { + + private static final Logger LOG = LogManager.getLogger(FeaturesAspect.class); + + @Around(value = "@within(featureAssociation) || @annotation(featureAssociation)") + public Object checkAspect(ProceedingJoinPoint joinPoint, FeatureAssociation featureAssociation) throws Throwable { + if (featureAssociation.value().isActive()) { + return joinPoint.proceed(); + } else { + LOG.info("Feature " + featureAssociation.value().name() + " is not enabled!"); + return null; + } + } + +} diff --git a/spring-boot-ops/src/main/java/com/baeldung/toggle/MyFeatures.java b/spring-boot-ops/src/main/java/com/baeldung/toggle/MyFeatures.java new file mode 100644 index 0000000000..a88ec2166e --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/toggle/MyFeatures.java @@ -0,0 +1,23 @@ +package com.baeldung.toggle; + +import org.togglz.core.Feature; +import org.togglz.core.activation.SystemPropertyActivationStrategy; +import org.togglz.core.annotation.ActivationParameter; +import org.togglz.core.annotation.DefaultActivationStrategy; +import org.togglz.core.annotation.EnabledByDefault; +import org.togglz.core.annotation.Label; +import org.togglz.core.context.FeatureContext; + +public enum MyFeatures implements Feature { + + @Label("Employee Management Feature") + @EnabledByDefault + @DefaultActivationStrategy(id = SystemPropertyActivationStrategy.ID, parameters = { @ActivationParameter(name = SystemPropertyActivationStrategy.PARAM_PROPERTY_NAME, value = "employee.feature"), + @ActivationParameter(name = SystemPropertyActivationStrategy.PARAM_PROPERTY_VALUE, value = "true") }) + EMPLOYEE_MANAGEMENT_FEATURE; + + public boolean isActive() { + return FeatureContext.getFeatureManager().isActive(this); + } + +} diff --git a/spring-boot-ops/src/main/java/com/baeldung/toggle/SalaryController.java b/spring-boot-ops/src/main/java/com/baeldung/toggle/SalaryController.java new file mode 100644 index 0000000000..5d72f0105a --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/toggle/SalaryController.java @@ -0,0 +1,20 @@ +package com.baeldung.toggle; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class SalaryController { + + @Autowired + SalaryService salaryService; + + @PostMapping(value = "/increaseSalary") + @ResponseBody + public void increaseSalary(@RequestParam long id) { + salaryService.increaseSalary(id); + } +} diff --git a/spring-boot-ops/src/main/java/com/baeldung/toggle/SalaryService.java b/spring-boot-ops/src/main/java/com/baeldung/toggle/SalaryService.java new file mode 100644 index 0000000000..48a1ddf8d8 --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/toggle/SalaryService.java @@ -0,0 +1,19 @@ +package com.baeldung.toggle; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class SalaryService { + + @Autowired + EmployeeRepository employeeRepository; + + @FeatureAssociation(value = MyFeatures.EMPLOYEE_MANAGEMENT_FEATURE) + public void increaseSalary(long id) { + Employee employee = employeeRepository.findById(id).orElse(null); + employee.setSalary(employee.getSalary() + employee.getSalary() * 0.1); + employeeRepository.save(employee); + } + +} diff --git a/spring-boot-ops/src/main/java/com/baeldung/toggle/ToggleApplication.java b/spring-boot-ops/src/main/java/com/baeldung/toggle/ToggleApplication.java new file mode 100644 index 0000000000..27be6b7cca --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/toggle/ToggleApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.toggle; + +import javax.annotation.security.RolesAllowed; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ToggleApplication { + @RolesAllowed("*") + public static void main(String[] args) { + System.setProperty("security.basic.enabled", "false"); + SpringApplication.run(ToggleApplication.class, args); + } +} diff --git a/spring-boot-ops/src/main/java/com/baeldung/toggle/ToggleConfiguration.java b/spring-boot-ops/src/main/java/com/baeldung/toggle/ToggleConfiguration.java new file mode 100644 index 0000000000..ee0b251479 --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/toggle/ToggleConfiguration.java @@ -0,0 +1,20 @@ +package com.baeldung.toggle; + +import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.togglz.core.manager.EnumBasedFeatureProvider; +import org.togglz.core.spi.FeatureProvider; + +@Configuration +@EnableJpaRepositories("com.baeldung.toggle") +@EntityScan("com.baeldung.toggle") +public class ToggleConfiguration { + + @Bean + public FeatureProvider featureProvider() { + return new EnumBasedFeatureProvider(MyFeatures.class); + } + +} diff --git a/spring-boot/src/main/java/com/baeldung/webjar/TestController.java b/spring-boot-ops/src/main/java/com/baeldung/webjar/TestController.java similarity index 100% rename from spring-boot/src/main/java/com/baeldung/webjar/TestController.java rename to spring-boot-ops/src/main/java/com/baeldung/webjar/TestController.java diff --git a/spring-boot/src/main/java/com/baeldung/webjar/WebjarsdemoApplication.java b/spring-boot-ops/src/main/java/com/baeldung/webjar/WebjarsdemoApplication.java similarity index 100% rename from spring-boot/src/main/java/com/baeldung/webjar/WebjarsdemoApplication.java rename to spring-boot-ops/src/main/java/com/baeldung/webjar/WebjarsdemoApplication.java diff --git a/spring-boot-ops/src/main/java/org/baeldung/boot/Application.java b/spring-boot-ops/src/main/java/org/baeldung/boot/Application.java new file mode 100644 index 0000000000..c1b6558b26 --- /dev/null +++ b/spring-boot-ops/src/main/java/org/baeldung/boot/Application.java @@ -0,0 +1,14 @@ +package org.baeldung.boot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; + +@SpringBootApplication +public class Application { + private static ApplicationContext applicationContext; + + public static void main(String[] args) { + applicationContext = SpringApplication.run(Application.class, args); + } +} diff --git a/spring-boot/src/main/java/org/baeldung/boot/config/WebConfig.java b/spring-boot-ops/src/main/java/org/baeldung/boot/config/WebConfig.java similarity index 100% rename from spring-boot/src/main/java/org/baeldung/boot/config/WebConfig.java rename to spring-boot-ops/src/main/java/org/baeldung/boot/config/WebConfig.java diff --git a/spring-boot/src/main/java/org/baeldung/boot/controller/GenericEntityController.java b/spring-boot-ops/src/main/java/org/baeldung/boot/controller/GenericEntityController.java similarity index 100% rename from spring-boot/src/main/java/org/baeldung/boot/controller/GenericEntityController.java rename to spring-boot-ops/src/main/java/org/baeldung/boot/controller/GenericEntityController.java diff --git a/spring-boot/src/main/java/org/baeldung/boot/converter/GenericBigDecimalConverter.java b/spring-boot-ops/src/main/java/org/baeldung/boot/converter/GenericBigDecimalConverter.java similarity index 100% rename from spring-boot/src/main/java/org/baeldung/boot/converter/GenericBigDecimalConverter.java rename to spring-boot-ops/src/main/java/org/baeldung/boot/converter/GenericBigDecimalConverter.java diff --git a/spring-boot/src/main/java/org/baeldung/boot/converter/StringToEmployeeConverter.java b/spring-boot-ops/src/main/java/org/baeldung/boot/converter/StringToEmployeeConverter.java similarity index 100% rename from spring-boot/src/main/java/org/baeldung/boot/converter/StringToEmployeeConverter.java rename to spring-boot-ops/src/main/java/org/baeldung/boot/converter/StringToEmployeeConverter.java diff --git a/spring-boot/src/main/java/org/baeldung/boot/converter/StringToEnumConverterFactory.java b/spring-boot-ops/src/main/java/org/baeldung/boot/converter/StringToEnumConverterFactory.java similarity index 100% rename from spring-boot/src/main/java/org/baeldung/boot/converter/StringToEnumConverterFactory.java rename to spring-boot-ops/src/main/java/org/baeldung/boot/converter/StringToEnumConverterFactory.java diff --git a/spring-boot/src/main/java/org/baeldung/boot/converter/StringToLocalDateTimeConverter.java b/spring-boot-ops/src/main/java/org/baeldung/boot/converter/StringToLocalDateTimeConverter.java similarity index 100% rename from spring-boot/src/main/java/org/baeldung/boot/converter/StringToLocalDateTimeConverter.java rename to spring-boot-ops/src/main/java/org/baeldung/boot/converter/StringToLocalDateTimeConverter.java diff --git a/spring-boot/src/main/java/org/baeldung/boot/converter/controller/StringToEmployeeConverterController.java b/spring-boot-ops/src/main/java/org/baeldung/boot/converter/controller/StringToEmployeeConverterController.java similarity index 100% rename from spring-boot/src/main/java/org/baeldung/boot/converter/controller/StringToEmployeeConverterController.java rename to spring-boot-ops/src/main/java/org/baeldung/boot/converter/controller/StringToEmployeeConverterController.java diff --git a/spring-boot-ops/src/main/java/org/baeldung/boot/domain/GenericEntity.java b/spring-boot-ops/src/main/java/org/baeldung/boot/domain/GenericEntity.java new file mode 100644 index 0000000000..f1c936e432 --- /dev/null +++ b/spring-boot-ops/src/main/java/org/baeldung/boot/domain/GenericEntity.java @@ -0,0 +1,42 @@ +package org.baeldung.boot.domain; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class GenericEntity { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String value; + + public GenericEntity() { + } + + public GenericEntity(String value) { + this.value = value; + } + + public GenericEntity(Long id, String value) { + this.id = id; + this.value = value; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/spring-boot-ops/src/main/java/org/baeldung/boot/domain/Modes.java b/spring-boot-ops/src/main/java/org/baeldung/boot/domain/Modes.java new file mode 100644 index 0000000000..dcba064e8c --- /dev/null +++ b/spring-boot-ops/src/main/java/org/baeldung/boot/domain/Modes.java @@ -0,0 +1,6 @@ +package org.baeldung.boot.domain; + +public enum Modes { + + ALPHA, BETA; +} diff --git a/spring-boot-ops/src/main/java/org/baeldung/boot/repository/GenericEntityRepository.java b/spring-boot-ops/src/main/java/org/baeldung/boot/repository/GenericEntityRepository.java new file mode 100644 index 0000000000..d897e17afe --- /dev/null +++ b/spring-boot-ops/src/main/java/org/baeldung/boot/repository/GenericEntityRepository.java @@ -0,0 +1,7 @@ +package org.baeldung.boot.repository; + +import org.baeldung.boot.domain.GenericEntity; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface GenericEntityRepository extends JpaRepository { +} diff --git a/spring-boot/src/main/java/org/baeldung/boot/web/resolver/HeaderVersionArgumentResolver.java b/spring-boot-ops/src/main/java/org/baeldung/boot/web/resolver/HeaderVersionArgumentResolver.java similarity index 100% rename from spring-boot/src/main/java/org/baeldung/boot/web/resolver/HeaderVersionArgumentResolver.java rename to spring-boot-ops/src/main/java/org/baeldung/boot/web/resolver/HeaderVersionArgumentResolver.java diff --git a/spring-boot/src/main/java/org/baeldung/boot/web/resolver/Version.java b/spring-boot-ops/src/main/java/org/baeldung/boot/web/resolver/Version.java similarity index 100% rename from spring-boot/src/main/java/org/baeldung/boot/web/resolver/Version.java rename to spring-boot-ops/src/main/java/org/baeldung/boot/web/resolver/Version.java diff --git a/spring-boot-ops/src/main/java/org/baeldung/demo/DemoApplication.java b/spring-boot-ops/src/main/java/org/baeldung/demo/DemoApplication.java new file mode 100644 index 0000000000..4a88fcea07 --- /dev/null +++ b/spring-boot-ops/src/main/java/org/baeldung/demo/DemoApplication.java @@ -0,0 +1,18 @@ +package org.baeldung.demo; + +import com.baeldung.graphql.GraphqlConfiguration; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import org.springframework.context.annotation.Import; + +@SpringBootApplication +@Import(GraphqlConfiguration.class) +public class DemoApplication { + + public static void main(String[] args) { + System.setProperty("spring.config.name", "demo"); + SpringApplication.run(DemoApplication.class, args); + } + +} diff --git a/spring-boot-ops/src/main/java/org/baeldung/demo/boottest/Employee.java b/spring-boot-ops/src/main/java/org/baeldung/demo/boottest/Employee.java new file mode 100644 index 0000000000..c1dd109f91 --- /dev/null +++ b/spring-boot-ops/src/main/java/org/baeldung/demo/boottest/Employee.java @@ -0,0 +1,43 @@ +package org.baeldung.demo.boottest; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.validation.constraints.Size; + +@Entity +@Table(name = "person") +public class Employee { + + public Employee() { + } + + public Employee(String name) { + this.name = name; + } + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @Size(min = 3, max = 20) + private String name; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/spring-boot-ops/src/main/java/org/baeldung/demo/boottest/EmployeeRepository.java b/spring-boot-ops/src/main/java/org/baeldung/demo/boottest/EmployeeRepository.java new file mode 100644 index 0000000000..00fdbfaae4 --- /dev/null +++ b/spring-boot-ops/src/main/java/org/baeldung/demo/boottest/EmployeeRepository.java @@ -0,0 +1,17 @@ +package org.baeldung.demo.boottest; + +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +@Repository +@Transactional +public interface EmployeeRepository extends JpaRepository { + + public Employee findByName(String name); + + public List findAll(); + +} diff --git a/spring-boot-ops/src/main/java/org/baeldung/demo/boottest/EmployeeRestController.java b/spring-boot-ops/src/main/java/org/baeldung/demo/boottest/EmployeeRestController.java new file mode 100644 index 0000000000..516bff0e8c --- /dev/null +++ b/spring-boot-ops/src/main/java/org/baeldung/demo/boottest/EmployeeRestController.java @@ -0,0 +1,33 @@ +package org.baeldung.demo.boottest; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api") +public class EmployeeRestController { + + @Autowired + private EmployeeService employeeService; + + @PostMapping("/employees") + public ResponseEntity createEmployee(@RequestBody Employee employee) { + HttpStatus status = HttpStatus.CREATED; + Employee saved = employeeService.save(employee); + return new ResponseEntity<>(saved, status); + } + + @GetMapping("/employees") + public List getAllEmployees() { + return employeeService.getAllEmployees(); + } + +} diff --git a/spring-boot-ops/src/main/java/org/baeldung/demo/boottest/EmployeeService.java b/spring-boot-ops/src/main/java/org/baeldung/demo/boottest/EmployeeService.java new file mode 100644 index 0000000000..07765a511c --- /dev/null +++ b/spring-boot-ops/src/main/java/org/baeldung/demo/boottest/EmployeeService.java @@ -0,0 +1,16 @@ +package org.baeldung.demo.boottest; + +import java.util.List; + +public interface EmployeeService { + + public Employee getEmployeeById(Long id); + + public Employee getEmployeeByName(String name); + + public List getAllEmployees(); + + public boolean exists(String email); + + public Employee save(Employee employee); +} diff --git a/spring-boot-ops/src/main/java/org/baeldung/demo/boottest/EmployeeServiceImpl.java b/spring-boot-ops/src/main/java/org/baeldung/demo/boottest/EmployeeServiceImpl.java new file mode 100644 index 0000000000..a1639b29cc --- /dev/null +++ b/spring-boot-ops/src/main/java/org/baeldung/demo/boottest/EmployeeServiceImpl.java @@ -0,0 +1,43 @@ +package org.baeldung.demo.boottest; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional +public class EmployeeServiceImpl implements EmployeeService { + + @Autowired + private EmployeeRepository employeeRepository; + + @Override + public Employee getEmployeeById(Long id) { + return employeeRepository.findById(id).orElse(null); + } + + @Override + public Employee getEmployeeByName(String name) { + return employeeRepository.findByName(name); + } + + @Override + public boolean exists(String name) { + if (employeeRepository.findByName(name) != null) { + return true; + } + return false; + } + + @Override + public Employee save(Employee employee) { + return employeeRepository.save(employee); + } + + @Override + public List getAllEmployees() { + return employeeRepository.findAll(); + } +} diff --git a/spring-boot-ops/src/main/java/org/baeldung/demo/components/FooService.java b/spring-boot-ops/src/main/java/org/baeldung/demo/components/FooService.java new file mode 100644 index 0000000000..66943f6461 --- /dev/null +++ b/spring-boot-ops/src/main/java/org/baeldung/demo/components/FooService.java @@ -0,0 +1,21 @@ +package org.baeldung.demo.components; + +import org.baeldung.demo.model.Foo; +import org.baeldung.demo.repository.FooRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class FooService { + + @Autowired + private FooRepository fooRepository; + + public Foo getFooWithId(Integer id) throws Exception { + return fooRepository.findById(id).orElse(null); + } + + public Foo getFooWithName(String name) { + return fooRepository.findByName(name); + } +} \ No newline at end of file diff --git a/spring-boot-ops/src/main/java/org/baeldung/demo/exceptions/CommonException.java b/spring-boot-ops/src/main/java/org/baeldung/demo/exceptions/CommonException.java new file mode 100644 index 0000000000..51dd7bbd44 --- /dev/null +++ b/spring-boot-ops/src/main/java/org/baeldung/demo/exceptions/CommonException.java @@ -0,0 +1,13 @@ +package org.baeldung.demo.exceptions; + +public class CommonException extends RuntimeException { + + /** + * + */ + private static final long serialVersionUID = 3080004140659213332L; + + public CommonException(String message) { + super(message); + } +} diff --git a/spring-boot-ops/src/main/java/org/baeldung/demo/exceptions/FooNotFoundException.java b/spring-boot-ops/src/main/java/org/baeldung/demo/exceptions/FooNotFoundException.java new file mode 100644 index 0000000000..59796c58f0 --- /dev/null +++ b/spring-boot-ops/src/main/java/org/baeldung/demo/exceptions/FooNotFoundException.java @@ -0,0 +1,13 @@ +package org.baeldung.demo.exceptions; + +public class FooNotFoundException extends RuntimeException { + + /** + * + */ + private static final long serialVersionUID = 9042200028456133589L; + + public FooNotFoundException(String message) { + super(message); + } +} diff --git a/spring-boot-ops/src/main/java/org/baeldung/demo/model/Foo.java b/spring-boot-ops/src/main/java/org/baeldung/demo/model/Foo.java new file mode 100644 index 0000000000..e5638cfd3d --- /dev/null +++ b/spring-boot-ops/src/main/java/org/baeldung/demo/model/Foo.java @@ -0,0 +1,45 @@ +package org.baeldung.demo.model; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Foo implements Serializable { + private static final long serialVersionUID = 1L; + @Id + @GeneratedValue + private Integer id; + private String name; + + public Foo() { + } + + public Foo(String name) { + this.name = name; + } + + public Foo(Integer id, String name) { + super(); + this.id = id; + this.name = name; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/spring-boot-ops/src/main/java/org/baeldung/demo/repository/FooRepository.java b/spring-boot-ops/src/main/java/org/baeldung/demo/repository/FooRepository.java new file mode 100644 index 0000000000..c04e0c7438 --- /dev/null +++ b/spring-boot-ops/src/main/java/org/baeldung/demo/repository/FooRepository.java @@ -0,0 +1,8 @@ +package org.baeldung.demo.repository; + +import org.baeldung.demo.model.Foo; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface FooRepository extends JpaRepository { + public Foo findByName(String name); +} diff --git a/spring-boot-ops/src/main/java/org/baeldung/demo/service/FooController.java b/spring-boot-ops/src/main/java/org/baeldung/demo/service/FooController.java new file mode 100644 index 0000000000..c28dcde1a7 --- /dev/null +++ b/spring-boot-ops/src/main/java/org/baeldung/demo/service/FooController.java @@ -0,0 +1,26 @@ +package org.baeldung.demo.service; + +import org.baeldung.demo.components.FooService; +import org.baeldung.demo.model.Foo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class FooController { + + @Autowired + private FooService fooService; + + @GetMapping("/{id}") + public Foo getFooWithId(@PathVariable Integer id) throws Exception { + return fooService.getFooWithId(id); + } + + @GetMapping("/") + public Foo getFooWithName(@RequestParam String name) throws Exception { + return fooService.getFooWithName(name); + } +} \ No newline at end of file diff --git a/spring-boot-ops/src/main/resources/application.properties b/spring-boot-ops/src/main/resources/application.properties deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/spring-boot/src/main/resources/templates/customer.html b/spring-boot-ops/src/main/resources/templates/customer.html similarity index 100% rename from spring-boot/src/main/resources/templates/customer.html rename to spring-boot-ops/src/main/resources/templates/customer.html diff --git a/spring-boot/src/main/resources/templates/customers.html b/spring-boot-ops/src/main/resources/templates/customers.html similarity index 100% rename from spring-boot/src/main/resources/templates/customers.html rename to spring-boot-ops/src/main/resources/templates/customers.html diff --git a/spring-boot/src/main/resources/templates/displayallbeans.html b/spring-boot-ops/src/main/resources/templates/displayallbeans.html similarity index 100% rename from spring-boot/src/main/resources/templates/displayallbeans.html rename to spring-boot-ops/src/main/resources/templates/displayallbeans.html diff --git a/spring-boot/src/main/resources/templates/error-404.html b/spring-boot-ops/src/main/resources/templates/error-404.html similarity index 100% rename from spring-boot/src/main/resources/templates/error-404.html rename to spring-boot-ops/src/main/resources/templates/error-404.html diff --git a/spring-boot/src/main/resources/templates/error-500.html b/spring-boot-ops/src/main/resources/templates/error-500.html similarity index 100% rename from spring-boot/src/main/resources/templates/error-500.html rename to spring-boot-ops/src/main/resources/templates/error-500.html diff --git a/spring-boot/src/main/resources/templates/error.html b/spring-boot-ops/src/main/resources/templates/error.html similarity index 100% rename from spring-boot/src/main/resources/templates/error.html rename to spring-boot-ops/src/main/resources/templates/error.html diff --git a/spring-boot/src/main/resources/templates/error/404.html b/spring-boot-ops/src/main/resources/templates/error/404.html similarity index 100% rename from spring-boot/src/main/resources/templates/error/404.html rename to spring-boot-ops/src/main/resources/templates/error/404.html diff --git a/spring-boot/src/main/resources/templates/external.html b/spring-boot-ops/src/main/resources/templates/external.html similarity index 100% rename from spring-boot/src/main/resources/templates/external.html rename to spring-boot-ops/src/main/resources/templates/external.html diff --git a/spring-boot/src/main/resources/templates/index.html b/spring-boot-ops/src/main/resources/templates/index.html similarity index 100% rename from spring-boot/src/main/resources/templates/index.html rename to spring-boot-ops/src/main/resources/templates/index.html diff --git a/spring-boot/src/main/resources/templates/international.html b/spring-boot-ops/src/main/resources/templates/international.html similarity index 100% rename from spring-boot/src/main/resources/templates/international.html rename to spring-boot-ops/src/main/resources/templates/international.html diff --git a/spring-boot/src/main/resources/templates/layout.html b/spring-boot-ops/src/main/resources/templates/layout.html similarity index 100% rename from spring-boot/src/main/resources/templates/layout.html rename to spring-boot-ops/src/main/resources/templates/layout.html diff --git a/spring-boot/src/main/resources/templates/other.html b/spring-boot-ops/src/main/resources/templates/other.html similarity index 100% rename from spring-boot/src/main/resources/templates/other.html rename to spring-boot-ops/src/main/resources/templates/other.html diff --git a/spring-boot/src/main/resources/templates/utils.html b/spring-boot-ops/src/main/resources/templates/utils.html similarity index 100% rename from spring-boot/src/main/resources/templates/utils.html rename to spring-boot-ops/src/main/resources/templates/utils.html diff --git a/spring-boot/src/test/java/com/baeldung/shutdown/ShutdownApplicationIntegrationTest.java b/spring-boot-ops/src/test/java/com/baeldung/shutdown/ShutdownApplicationIntegrationTest.java similarity index 100% rename from spring-boot/src/test/java/com/baeldung/shutdown/ShutdownApplicationIntegrationTest.java rename to spring-boot-ops/src/test/java/com/baeldung/shutdown/ShutdownApplicationIntegrationTest.java diff --git a/spring-boot/src/test/java/com/baeldung/webjar/WebjarsdemoApplicationIntegrationTest.java b/spring-boot-ops/src/test/java/com/baeldung/webjar/WebjarsdemoApplicationIntegrationTest.java similarity index 100% rename from spring-boot/src/test/java/com/baeldung/webjar/WebjarsdemoApplicationIntegrationTest.java rename to spring-boot-ops/src/test/java/com/baeldung/webjar/WebjarsdemoApplicationIntegrationTest.java diff --git a/spring-boot/src/test/java/org/baeldung/SpringBootApplicationIntegrationTest.java b/spring-boot-ops/src/test/java/org/baeldung/SpringBootApplicationIntegrationTest.java similarity index 100% rename from spring-boot/src/test/java/org/baeldung/SpringBootApplicationIntegrationTest.java rename to spring-boot-ops/src/test/java/org/baeldung/SpringBootApplicationIntegrationTest.java diff --git a/spring-boot/src/test/java/org/baeldung/SpringBootJPAIntegrationTest.java b/spring-boot-ops/src/test/java/org/baeldung/SpringBootJPAIntegrationTest.java similarity index 100% rename from spring-boot/src/test/java/org/baeldung/SpringBootJPAIntegrationTest.java rename to spring-boot-ops/src/test/java/org/baeldung/SpringBootJPAIntegrationTest.java diff --git a/spring-boot/src/test/java/org/baeldung/SpringBootMailIntegrationTest.java b/spring-boot-ops/src/test/java/org/baeldung/SpringBootMailIntegrationTest.java similarity index 100% rename from spring-boot/src/test/java/org/baeldung/SpringBootMailIntegrationTest.java rename to spring-boot-ops/src/test/java/org/baeldung/SpringBootMailIntegrationTest.java diff --git a/spring-boot-ops/src/test/resources/application-integrationtest.properties b/spring-boot-ops/src/test/resources/application-integrationtest.properties new file mode 100644 index 0000000000..bcd03226d3 --- /dev/null +++ b/spring-boot-ops/src/test/resources/application-integrationtest.properties @@ -0,0 +1,4 @@ +spring.datasource.url=jdbc:mysql://localhost:3306/employee_int_test +spring.datasource.username=root +spring.datasource.password=root + diff --git a/spring-boot-ops/src/test/resources/application.properties b/spring-boot-ops/src/test/resources/application.properties new file mode 100644 index 0000000000..bda75c25fa --- /dev/null +++ b/spring-boot-ops/src/test/resources/application.properties @@ -0,0 +1,9 @@ +spring.mail.host=localhost +spring.mail.port=8025 +spring.mail.properties.mail.smtp.auth=false + +security.basic.enabled=false + +management.endpoints.web.exposure.include=* +management.endpoint.shutdown.enabled=true +endpoints.shutdown.enabled=true \ No newline at end of file diff --git a/spring-boot/README.MD b/spring-boot/README.MD index 094a70654a..3574c1be36 100644 --- a/spring-boot/README.MD +++ b/spring-boot/README.MD @@ -4,12 +4,10 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles: - [Quick Guide to @RestClientTest in Spring Boot](http://www.baeldung.com/restclienttest-in-spring-boot) -- [Intro to Spring Boot Starters](http://www.baeldung.com/spring-boot-starters) - [A Guide to Spring in Eclipse STS](http://www.baeldung.com/eclipse-sts-spring) - [Introduction to WebJars](http://www.baeldung.com/maven-webjars) - [Create a Fat Jar App with Spring Boot](http://www.baeldung.com/deployable-fat-jar-spring-boot) - [The @ServletComponentScan Annotation in Spring Boot](http://www.baeldung.com/spring-servletcomponentscan) -- [A Custom Data Binder in Spring MVC](http://www.baeldung.com/spring-mvc-custom-data-binder) - [Intro to Building an Application with Spring Boot](http://www.baeldung.com/intro-to-spring-boot) - [How to Register a Servlet in a Java Web Application](http://www.baeldung.com/register-servlet) - [Guide to Spring WebUtils and ServletRequestUtils](http://www.baeldung.com/spring-webutils-servletrequestutils) diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index afc80eb68b..c1b21b9b5e 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -38,11 +38,6 @@ graphql-spring-boot-starter 3.6.0 - - com.graphql-java - graphiql-spring-boot-starter - 3.6.0 - com.graphql-java graphql-java-tools @@ -84,27 +79,6 @@ json-path test - - org.springframework.boot - spring-boot-starter-mail - - - org.subethamail - subethasmtp - ${subethasmtp.version} - test - - - - org.webjars - bootstrap - ${bootstrap.version} - - - org.webjars - jquery - ${jquery.version} - com.google.guava @@ -218,9 +192,6 @@ org.baeldung.demo.DemoApplication - 3.1.1 - 3.3.7-1 - 3.1.7 8.5.11 2.4.1.Final 1.9.0