BAEL-1800 (#4377)
* BAEL-1800 - Code for http://www.baeldung.com/spring-boot-starters and http://www.baeldung.com/spring-mvc-custom-data-binder * BAEL-1800 - Code for http://www.baeldung.com/maven-webjars and http://www.baeldung.com/deployable-fat-jar-spring-boot * BAEL-1800 - Code for http://www.baeldung.com/spring-boot-shutdown * BAEL-1800 - Code for http://www.baeldung.com/spring-boot-shutdown * BAEL-1800 - Code for http://www.baeldung.com/spring-boot-dependency-management-custom-parent
This commit is contained in:
committed by
maibin
parent
8e36e2b155
commit
6e94f0343c
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.graphql;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class AuthorDao {
|
||||
private List<Author> authors;
|
||||
|
||||
public AuthorDao(List<Author> authors) {
|
||||
this.authors = authors;
|
||||
}
|
||||
|
||||
public Optional<Author> getAuthor(String id) {
|
||||
return authors.stream().filter(author -> id.equals(author.getId())).findFirst();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.graphql;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.coxautodev.graphql.tools.GraphQLResolver;
|
||||
|
||||
public class AuthorResolver implements GraphQLResolver<Author> {
|
||||
private PostDao postDao;
|
||||
|
||||
public AuthorResolver(PostDao postDao) {
|
||||
this.postDao = postDao;
|
||||
}
|
||||
|
||||
public List<Post> getPosts(Author author) {
|
||||
return postDao.getAuthorPosts(author.getId());
|
||||
}
|
||||
}
|
||||
@@ -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<Post> 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<Author> 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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
49
spring-boot-ops/src/main/java/com/baeldung/graphql/Post.java
Normal file
49
spring-boot-ops/src/main/java/com/baeldung/graphql/Post.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.graphql;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PostDao {
|
||||
private List<Post> posts;
|
||||
|
||||
public PostDao(List<Post> posts) {
|
||||
this.posts = posts;
|
||||
}
|
||||
|
||||
public List<Post> getRecentPosts(int count, int offset) {
|
||||
return posts.stream().skip(offset).limit(count).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Post> getAuthorPosts(String author) {
|
||||
return posts.stream().filter(post -> author.equals(post.getAuthorId())).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public void savePost(Post post) {
|
||||
posts.add(0, post);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.graphql;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import com.coxautodev.graphql.tools.GraphQLResolver;
|
||||
|
||||
public class PostResolver implements GraphQLResolver<Post> {
|
||||
private AuthorDao authorDao;
|
||||
|
||||
public PostResolver(AuthorDao authorDao) {
|
||||
this.authorDao = authorDao;
|
||||
}
|
||||
|
||||
public Optional<Author> getAuthor(Post post) {
|
||||
return authorDao.getAuthor(post.getAuthorId());
|
||||
}
|
||||
}
|
||||
@@ -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<Post> recentPosts(int count, int offset) {
|
||||
return postDao.getRecentPosts(count, offset);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.shutdown;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.context.ApplicationPidFileWriter;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
SpringApplication.run(Application.class, args);
|
||||
// closeApplication();
|
||||
// exitApplication();
|
||||
// writePID();
|
||||
|
||||
}
|
||||
|
||||
private static void closeApplication() {
|
||||
|
||||
ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class).web(WebApplicationType.NONE).run();
|
||||
System.out.println("Spring Boot application started");
|
||||
ctx.getBean(TerminateBean.class);
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
private static void exitApplication() {
|
||||
|
||||
ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class).web(WebApplicationType.NONE).run();
|
||||
|
||||
int exitCode = SpringApplication.exit(ctx, () -> {
|
||||
// return the error code
|
||||
return 0;
|
||||
});
|
||||
|
||||
System.out.println("Exit Spring Boot");
|
||||
|
||||
System.exit(exitCode);
|
||||
}
|
||||
|
||||
private static void writePID() {
|
||||
SpringApplicationBuilder app = new SpringApplicationBuilder(Application.class).web(WebApplicationType.NONE);
|
||||
app.build().addListeners(new ApplicationPidFileWriter("./bin/shutdown.pid"));
|
||||
app.run();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.shutdown;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = "com.baeldung.shutdown")
|
||||
public class ShutdownConfig {
|
||||
|
||||
@Bean
|
||||
public TerminateBean getTerminateBean() {
|
||||
return new TerminateBean();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.shutdown;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
public class TerminateBean {
|
||||
|
||||
@PreDestroy
|
||||
public void onDestroy() throws Exception {
|
||||
System.out.println("Spring Container is destroyed!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.shutdown.shutdown;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class ShutdownController implements ApplicationContextAware {
|
||||
|
||||
private ApplicationContext context;
|
||||
|
||||
@PostMapping("/shutdownContext")
|
||||
public void shutdownContext() {
|
||||
((ConfigurableApplicationContext) context).close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
|
||||
this.context = ctx;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.toggle;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface EmployeeRepository extends CrudRepository<Employee, Long> {
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.webjar;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
@Controller
|
||||
public class TestController {
|
||||
|
||||
@GetMapping(value = "/")
|
||||
public String welcome(Model model) {
|
||||
return "index";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.webjar;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class WebjarsdemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(WebjarsdemoApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.baeldung.boot.config;
|
||||
|
||||
import org.baeldung.boot.converter.GenericBigDecimalConverter;
|
||||
import org.baeldung.boot.converter.StringToEmployeeConverter;
|
||||
import org.baeldung.boot.converter.StringToEnumConverterFactory;
|
||||
import org.baeldung.boot.web.resolver.HeaderVersionArgumentResolver;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.format.FormatterRegistry;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addArgumentResolvers(final List<HandlerMethodArgumentResolver> argumentResolvers) {
|
||||
argumentResolvers.add(new HeaderVersionArgumentResolver());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addFormatters(FormatterRegistry registry) {
|
||||
registry.addConverter(new StringToEmployeeConverter());
|
||||
registry.addConverterFactory(new StringToEnumConverterFactory());
|
||||
registry.addConverter(new GenericBigDecimalConverter());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package org.baeldung.boot.controller;
|
||||
|
||||
import org.baeldung.boot.domain.GenericEntity;
|
||||
import org.baeldung.boot.domain.Modes;
|
||||
import org.baeldung.boot.web.resolver.Version;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
public class GenericEntityController {
|
||||
private List<GenericEntity> entityList = new ArrayList<>();
|
||||
|
||||
{
|
||||
entityList.add(new GenericEntity(1l, "entity_1"));
|
||||
entityList.add(new GenericEntity(2l, "entity_2"));
|
||||
entityList.add(new GenericEntity(3l, "entity_3"));
|
||||
entityList.add(new GenericEntity(4l, "entity_4"));
|
||||
}
|
||||
|
||||
@GetMapping("/entity/all")
|
||||
public List<GenericEntity> findAll() {
|
||||
return entityList;
|
||||
}
|
||||
|
||||
@PostMapping("/entity")
|
||||
public GenericEntity addEntity(GenericEntity entity) {
|
||||
entityList.add(entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@GetMapping("/entity/findby/{id}")
|
||||
public GenericEntity findById(@PathVariable Long id) {
|
||||
return entityList.stream().filter(entity -> entity.getId().equals(id)).findFirst().get();
|
||||
}
|
||||
|
||||
@GetMapping("/entity/findbydate/{date}")
|
||||
public GenericEntity findByDate(@PathVariable("date") LocalDateTime date) {
|
||||
return entityList.stream().findFirst().get();
|
||||
}
|
||||
|
||||
@GetMapping("/entity/findbymode/{mode}")
|
||||
public GenericEntity findByEnum(@PathVariable("mode") Modes mode) {
|
||||
return entityList.stream().findFirst().get();
|
||||
}
|
||||
|
||||
@GetMapping("/entity/findbyversion")
|
||||
public ResponseEntity findByVersion(@Version String version) {
|
||||
return version != null ? new ResponseEntity(entityList.stream().findFirst().get(), HttpStatus.OK) : new ResponseEntity(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.baeldung.boot.converter;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.converter.GenericConverter;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Set;
|
||||
|
||||
public class GenericBigDecimalConverter implements GenericConverter {
|
||||
@Override
|
||||
public Set<ConvertiblePair> getConvertibleTypes() {
|
||||
|
||||
ConvertiblePair[] pairs = new ConvertiblePair[] { new ConvertiblePair(Number.class, BigDecimal.class), new ConvertiblePair(String.class, BigDecimal.class) };
|
||||
|
||||
return ImmutableSet.copyOf(pairs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
if (sourceType.getType() == BigDecimal.class) {
|
||||
return source;
|
||||
}
|
||||
|
||||
if (sourceType.getType() == String.class) {
|
||||
String number = (String) source;
|
||||
return new BigDecimal(number);
|
||||
} else {
|
||||
Number number = (Number) source;
|
||||
BigDecimal converted = new BigDecimal(number.doubleValue());
|
||||
return converted.setScale(2, BigDecimal.ROUND_HALF_EVEN);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.baeldung.boot.converter;
|
||||
|
||||
import com.baeldung.toggle.Employee;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
|
||||
public class StringToEmployeeConverter implements Converter<String, Employee> {
|
||||
|
||||
@Override
|
||||
public Employee convert(String from) {
|
||||
String[] data = from.split(",");
|
||||
return new Employee(Long.parseLong(data[0]), Double.parseDouble(data[1]));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.baeldung.boot.converter;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.converter.ConverterFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class StringToEnumConverterFactory implements ConverterFactory<String, Enum> {
|
||||
|
||||
private static class StringToEnumConverter<T extends Enum> implements Converter<String, T> {
|
||||
|
||||
private Class<T> enumType;
|
||||
|
||||
public StringToEnumConverter(Class<T> enumType) {
|
||||
this.enumType = enumType;
|
||||
}
|
||||
|
||||
public T convert(String source) {
|
||||
return (T) Enum.valueOf(this.enumType, source.trim());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Enum> Converter<String, T> getConverter(final Class<T> targetType) {
|
||||
return new StringToEnumConverter(targetType);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.baeldung.boot.converter;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
@Component
|
||||
public class StringToLocalDateTimeConverter implements Converter<String, LocalDateTime> {
|
||||
|
||||
@Override
|
||||
public LocalDateTime convert(final String source) {
|
||||
return LocalDateTime.parse(source, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.baeldung.boot.converter.controller;
|
||||
|
||||
import com.baeldung.toggle.Employee;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/string-to-employee")
|
||||
public class StringToEmployeeConverterController {
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<Object> getStringToEmployee(@RequestParam("employee") Employee employee) {
|
||||
return ResponseEntity.ok(employee);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package org.baeldung.boot.domain;
|
||||
|
||||
public enum Modes {
|
||||
|
||||
ALPHA, BETA;
|
||||
}
|
||||
@@ -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<GenericEntity, Long> {
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.baeldung.boot.web.resolver;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@Component
|
||||
public class HeaderVersionArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
|
||||
@Override
|
||||
public boolean supportsParameter(final MethodParameter methodParameter) {
|
||||
return methodParameter.getParameterAnnotation(Version.class) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolveArgument(final MethodParameter methodParameter, final ModelAndViewContainer modelAndViewContainer, final NativeWebRequest nativeWebRequest, final WebDataBinderFactory webDataBinderFactory) throws Exception {
|
||||
HttpServletRequest request = (HttpServletRequest) nativeWebRequest.getNativeRequest();
|
||||
|
||||
return request.getHeader("Version");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.baeldung.boot.web.resolver;
|
||||
|
||||
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.PARAMETER)
|
||||
public @interface Version {
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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<Employee, Long> {
|
||||
|
||||
public Employee findByName(String name);
|
||||
|
||||
public List<Employee> findAll();
|
||||
|
||||
}
|
||||
@@ -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<Employee> createEmployee(@RequestBody Employee employee) {
|
||||
HttpStatus status = HttpStatus.CREATED;
|
||||
Employee saved = employeeService.save(employee);
|
||||
return new ResponseEntity<>(saved, status);
|
||||
}
|
||||
|
||||
@GetMapping("/employees")
|
||||
public List<Employee> getAllEmployees() {
|
||||
return employeeService.getAllEmployees();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<Employee> getAllEmployees();
|
||||
|
||||
public boolean exists(String email);
|
||||
|
||||
public Employee save(Employee employee);
|
||||
}
|
||||
@@ -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<Employee> getAllEmployees() {
|
||||
return employeeRepository.findAll();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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<Foo, Integer> {
|
||||
public Foo findByName(String name);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
16
spring-boot-ops/src/main/resources/templates/customer.html
Normal file
16
spring-boot-ops/src/main/resources/templates/customer.html
Normal file
@@ -0,0 +1,16 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
|
||||
<title>Customer Page</title>
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<br />
|
||||
<form action="customer" method="POST">
|
||||
Contact Info: <input type="text" name="contactInfo" /> <br />
|
||||
<input type="submit" value="Submit" />
|
||||
</form>
|
||||
<br /><br />
|
||||
<span th:text="${message}"></span><br />
|
||||
</body>
|
||||
</html>
|
||||
33
spring-boot-ops/src/main/resources/templates/customers.html
Normal file
33
spring-boot-ops/src/main/resources/templates/customers.html
Normal file
@@ -0,0 +1,33 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head th:include="layout :: headerFragment">
|
||||
</head>
|
||||
<body>
|
||||
<div id="container">
|
||||
<h1>
|
||||
Hello, <span th:text="${username}">--name--</span>.
|
||||
</h1>
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Name</th>
|
||||
<th>Address</th>
|
||||
<th>Service Rendered</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="customer : ${customers}">
|
||||
<td th:text="${customer.id}">Text ...</td>
|
||||
<td th:text="${customer.name}">Text ...</td>
|
||||
<td th:text="${customer.address}">Text ...</td>
|
||||
<td th:text="${customer.serviceRendered}">Text...</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div id="pagefoot" th:include="layout :: footerFragment">Footer
|
||||
</div>
|
||||
</div>
|
||||
<!-- container -->
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,10 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
|
||||
<title>Baeldung</title>
|
||||
</head>
|
||||
<body>
|
||||
<h2 th:text="${header}"></h2>
|
||||
<p th:text="${message}"></p>
|
||||
</body>
|
||||
</html>
|
||||
14
spring-boot-ops/src/main/resources/templates/error-404.html
Normal file
14
spring-boot-ops/src/main/resources/templates/error-404.html
Normal file
@@ -0,0 +1,14 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous" />
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="jumbotron" >
|
||||
<h1 class="text-center"><i class="fa fa-frown-o"> </i> Sorry, we couldn't find the page you were looking for. </h1>
|
||||
<p class="text-center"><a class="btn btn-primary" href="/"><i class="fa fa-home"></i>Go Home</a></p>
|
||||
</div></div>
|
||||
</body>
|
||||
</html>
|
||||
16
spring-boot-ops/src/main/resources/templates/error-500.html
Normal file
16
spring-boot-ops/src/main/resources/templates/error-500.html
Normal file
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous" />
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="jumbotron" >
|
||||
<h1 class="text-center"><i class="fa fa-frown-o"> </i> Sorry, something went wrong! </h1>
|
||||
|
||||
<h2 class="text-center">We're fixing it.</h2>
|
||||
<p class="text-center"><a class="btn btn-primary" href="/"><i class="fa fa-home"></i>Go Home</a></p>
|
||||
</div></div>
|
||||
</body>
|
||||
</html>
|
||||
16
spring-boot-ops/src/main/resources/templates/error.html
Normal file
16
spring-boot-ops/src/main/resources/templates/error.html
Normal file
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous" />
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="jumbotron" >
|
||||
<h1 class="text-center"><i class="fa fa-frown-o"> </i> Something went wrong! </h1>
|
||||
|
||||
<h2 class="text-center">Our Engineers are on it.</h2>
|
||||
<p class="text-center"><a class="btn btn-primary" href="/"><i class="fa fa-home"></i>Go Home</a></p>
|
||||
</div></div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,8 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>RESOURCE NOT FOUND</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>404 RESOURCE NOT FOUND</h1>
|
||||
</body>
|
||||
</html>
|
||||
31
spring-boot-ops/src/main/resources/templates/external.html
Normal file
31
spring-boot-ops/src/main/resources/templates/external.html
Normal file
@@ -0,0 +1,31 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head th:include="layout :: headerFragment">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="jumbotron text-center">
|
||||
<h1>Customer Portal</h1>
|
||||
</div>
|
||||
<div>
|
||||
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam
|
||||
erat lectus, vehicula feugiat ultricies at, tempus sed ante. Cras
|
||||
arcu erat, lobortis vitae quam et, mollis pharetra odio. Nullam sit
|
||||
amet congue ipsum. Nunc dapibus odio ut ligula venenatis porta non
|
||||
id dui. Duis nec tempor tellus. Suspendisse id blandit ligula, sit
|
||||
amet varius mauris. Nulla eu eros pharetra, tristique dui quis,
|
||||
vehicula libero. Aenean a neque sit amet tellus porttitor rutrum nec
|
||||
at leo.</p>
|
||||
|
||||
<h2>Existing Customers</h2>
|
||||
<div class="well">
|
||||
<b>Enter the intranet: </b><a th:href="@{/customers}">customers</a>
|
||||
</div>
|
||||
</div>
|
||||
<div id="pagefoot" th:include="layout :: footerFragment">Footer
|
||||
</div>
|
||||
</div>
|
||||
<!-- container -->
|
||||
|
||||
</body>
|
||||
</html>
|
||||
19
spring-boot-ops/src/main/resources/templates/index.html
Normal file
19
spring-boot-ops/src/main/resources/templates/index.html
Normal file
@@ -0,0 +1,19 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>WebJars Demo</title>
|
||||
<link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css" />
|
||||
</head>
|
||||
<body>
|
||||
<h1>Welcome Home</h1>
|
||||
<div class="container"><br/>
|
||||
<div class="alert alert-success">
|
||||
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
|
||||
<strong>Success!</strong> It is working as we expected.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="/webjars/jquery/3.1.1/jquery.min.js"></script>
|
||||
<script src="/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js"></script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,20 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="ISO-8859-1" />
|
||||
<title>Home</title>
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
|
||||
<script src="internationalization.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1 th:text="#{greeting}"></h1>
|
||||
|
||||
<br /><br />
|
||||
<span th:text="#{lang.change}"></span>:
|
||||
<select id="locales">
|
||||
<option value=""></option>
|
||||
<option value="en" th:text="#{lang.eng}"></option>
|
||||
<option value="fr" th:text="#{lang.fr}"></option>
|
||||
</select>
|
||||
</body>
|
||||
</html>
|
||||
18
spring-boot-ops/src/main/resources/templates/layout.html
Normal file
18
spring-boot-ops/src/main/resources/templates/layout.html
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
<head th:fragment="headerFragment">
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
<title>Customer Portal</title>
|
||||
<link
|
||||
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
|
||||
rel="stylesheet"
|
||||
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
|
||||
crossorigin="anonymous"></link>
|
||||
<link
|
||||
href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css"
|
||||
rel="stylesheet"></link>
|
||||
</head>
|
||||
|
||||
<div id="pagefoot" th:fragment="footerFragment">
|
||||
<p>Document last modified 2017/10/23.</p>
|
||||
<p>Copyright: Lorem Ipsum</p>
|
||||
</div>
|
||||
16
spring-boot-ops/src/main/resources/templates/other.html
Normal file
16
spring-boot-ops/src/main/resources/templates/other.html
Normal file
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<title>Spring Utils Demo</title>
|
||||
<style type="text/css">
|
||||
.param{
|
||||
color:green;
|
||||
font-style: italic;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
Parameter set by you: <p th:text="${parameter}" class="param"/>
|
||||
</body>
|
||||
</html>
|
||||
23
spring-boot-ops/src/main/resources/templates/utils.html
Normal file
23
spring-boot-ops/src/main/resources/templates/utils.html
Normal file
@@ -0,0 +1,23 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<title>Spring Utils Demo</title>
|
||||
<style type="text/css">
|
||||
.param{
|
||||
color:green;
|
||||
font-style: italic;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<form action="setParam" method="POST">
|
||||
<h3>Set Parameter: </h3>
|
||||
<p th:text="${parameter}" class="param"/>
|
||||
<input type="text" name="param" id="param"/>
|
||||
<input type="submit" value="SET"/>
|
||||
</form>
|
||||
<br/>
|
||||
<a href="other">Another Page</a>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.shutdown;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, classes = Application.class)
|
||||
@AutoConfigureMockMvc
|
||||
public class ShutdownApplicationIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void givenBootApp_whenShutdownEndpoint_thenExit() throws Exception {
|
||||
|
||||
mockMvc.perform(post("/shutdown")).andExpect(status().isOk());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.webjar;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = WebjarsdemoApplication.class)
|
||||
@WebAppConfiguration
|
||||
public class WebjarsdemoApplicationIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package org.baeldung;
|
||||
|
||||
import org.baeldung.boot.Application;
|
||||
import org.baeldung.boot.domain.Modes;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
@WebAppConfiguration
|
||||
public class SpringBootApplicationIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webApplicationContext;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setupMockMvc() {
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRequestHasBeenMade_whenMeetsAllOfGivenConditions_thenCorrect() throws Exception {
|
||||
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/entity/all")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType)).andExpect(jsonPath("$", hasSize(4)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRequestHasBeenMade_whenMeetsFindByDateOfGivenConditions_thenCorrect() throws Exception {
|
||||
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbydate/{date}", "2011-12-03T10:15:30")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType))
|
||||
.andExpect(jsonPath("$.id", equalTo(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRequestHasBeenMade_whenMeetsFindByModeOfGivenConditions_thenCorrect() throws Exception {
|
||||
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbymode/{mode}", Modes.ALPHA.name())).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType)).andExpect(jsonPath("$.id", equalTo(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRequestHasBeenMade_whenMeetsFindByVersionOfGivenConditions_thenCorrect() throws Exception {
|
||||
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbyversion").header("Version", "1.0.0")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType))
|
||||
.andExpect(jsonPath("$.id", equalTo(1)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.baeldung;
|
||||
|
||||
import org.baeldung.boot.Application;
|
||||
import org.baeldung.boot.domain.GenericEntity;
|
||||
import org.baeldung.boot.repository.GenericEntityRepository;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
public class SpringBootJPAIntegrationTest {
|
||||
@Autowired
|
||||
private GenericEntityRepository genericEntityRepository;
|
||||
|
||||
@Test
|
||||
public void givenGenericEntityRepository_whenSaveAndRetreiveEntity_thenOK() {
|
||||
GenericEntity genericEntity = genericEntityRepository.save(new GenericEntity("test"));
|
||||
GenericEntity foundEntity = genericEntityRepository.findById(genericEntity.getId()).orElse(null);
|
||||
assertNotNull(foundEntity);
|
||||
assertEquals(genericEntity.getValue(), foundEntity.getValue());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package org.baeldung;
|
||||
|
||||
import org.baeldung.boot.Application;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.subethamail.wiser.Wiser;
|
||||
import org.subethamail.wiser.WiserMessage;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
public class SpringBootMailIntegrationTest {
|
||||
@Autowired
|
||||
private JavaMailSender javaMailSender;
|
||||
|
||||
private Wiser wiser;
|
||||
|
||||
private String userTo = "user2@localhost";
|
||||
private String userFrom = "user1@localhost";
|
||||
private String subject = "Test subject";
|
||||
private String textMail = "Text subject mail";
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
final int TEST_PORT = 8025;
|
||||
wiser = new Wiser(TEST_PORT);
|
||||
wiser.start();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
wiser.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMail_whenSendAndReceived_thenCorrect() throws Exception {
|
||||
SimpleMailMessage message = composeEmailMessage();
|
||||
javaMailSender.send(message);
|
||||
List<WiserMessage> messages = wiser.getMessages();
|
||||
|
||||
assertThat(messages, hasSize(1));
|
||||
WiserMessage wiserMessage = messages.get(0);
|
||||
assertEquals(userFrom, wiserMessage.getEnvelopeSender());
|
||||
assertEquals(userTo, wiserMessage.getEnvelopeReceiver());
|
||||
assertEquals(subject, getSubject(wiserMessage));
|
||||
assertEquals(textMail, getMessage(wiserMessage));
|
||||
}
|
||||
|
||||
private String getMessage(WiserMessage wiserMessage) throws MessagingException, IOException {
|
||||
return wiserMessage.getMimeMessage().getContent().toString().trim();
|
||||
}
|
||||
|
||||
private String getSubject(WiserMessage wiserMessage) throws MessagingException {
|
||||
return wiserMessage.getMimeMessage().getSubject();
|
||||
}
|
||||
|
||||
private SimpleMailMessage composeEmailMessage() {
|
||||
SimpleMailMessage mailMessage = new SimpleMailMessage();
|
||||
mailMessage.setTo(userTo);
|
||||
mailMessage.setReplyTo(userFrom);
|
||||
mailMessage.setFrom(userFrom);
|
||||
mailMessage.setSubject(subject);
|
||||
mailMessage.setText(textMail);
|
||||
return mailMessage;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/employee_int_test
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=root
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user