Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -41,6 +41,7 @@
|
||||
<module>spring-boot-environment</module>
|
||||
<module>spring-boot-exceptions</module>
|
||||
<module>spring-boot-flowable</module>
|
||||
<module>spring-boot-groovy</module>
|
||||
<!-- <module>spring-boot-gradle</module> --> <!-- Not a maven project -->
|
||||
<module>spring-boot-jasypt</module>
|
||||
<module>spring-boot-keycloak</module>
|
||||
@@ -61,6 +62,7 @@
|
||||
<module>spring-boot-runtime</module>
|
||||
<module>spring-boot-security</module>
|
||||
<module>spring-boot-springdoc</module>
|
||||
<module>spring-boot-swagger</module>
|
||||
<module>spring-boot-testing</module>
|
||||
<module>spring-boot-vue</module>
|
||||
<module>spring-boot-xml</module>
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
<description>Module for Spring Boot version 1.x</description>
|
||||
|
||||
<parent>
|
||||
<!-- This module contains article about Spring Boot Actuator in Spring Boot version 1.x -->
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-1</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
+1
@@ -8,3 +8,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
### Relevant Articles:
|
||||
|
||||
- [Liveness and Readiness Probes in Spring Boot](https://www.baeldung.com/spring-liveness-readiness-probes)
|
||||
- [Custom Information in Spring Boot Info Endpoint](https://www.baeldung.com/spring-boot-info-actuator-custom)
|
||||
@@ -24,6 +24,14 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.endpoints.info;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
-1
@@ -3,7 +3,6 @@ package com.baeldung.endpoints.info;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.baeldung.repository.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.info.Info;
|
||||
import org.springframework.boot.actuate.info.InfoContributor;
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.endpoints.info;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Integer id;
|
||||
private String name;
|
||||
private Integer status;
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(String name, Integer status) {
|
||||
this.name = name;
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Integer status) {
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package com.baeldung.endpoints.info;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Repository("userRepository")
|
||||
public interface UserRepository extends JpaRepository<User, Integer> {
|
||||
|
||||
int countByStatus(int status);
|
||||
|
||||
Optional<User> findOneByName(String name);
|
||||
|
||||
@Async
|
||||
CompletableFuture<User> findOneByStatus(Integer status);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.status = 1")
|
||||
Collection<User> findAllActiveUsers();
|
||||
|
||||
@Query(value = "SELECT * FROM USERS u WHERE u.status = 1", nativeQuery = true)
|
||||
Collection<User> findAllActiveUsersNative();
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.status = ?1")
|
||||
User findUserByStatus(Integer status);
|
||||
|
||||
@Query(value = "SELECT * FROM Users u WHERE u.status = ?1", nativeQuery = true)
|
||||
User findUserByStatusNative(Integer status);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.status = ?1 and u.name = ?2")
|
||||
User findUserByStatusAndName(Integer status, String name);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.status = :status and u.name = :name")
|
||||
User findUserByStatusAndNameNamedParams(@Param("status") Integer status, @Param("name") String name);
|
||||
|
||||
@Query(value = "SELECT * FROM Users u WHERE u.status = :status AND u.name = :name", nativeQuery = true)
|
||||
User findUserByStatusAndNameNamedParamsNative(@Param("status") Integer status, @Param("name") String name);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.status = :status and u.name = :name")
|
||||
User findUserByUserStatusAndUserName(@Param("status") Integer userStatus, @Param("name") String userName);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.name like ?1%")
|
||||
User findUserByNameLike(String name);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.name like :name%")
|
||||
User findUserByNameLikeNamedParam(@Param("name") String name);
|
||||
|
||||
@Query(value = "SELECT * FROM users u WHERE u.name LIKE ?1%", nativeQuery = true)
|
||||
User findUserByNameLikeNative(String name);
|
||||
|
||||
@Query(value = "SELECT u FROM User u")
|
||||
List<User> findAllUsers(Sort sort);
|
||||
|
||||
@Query(value = "SELECT u FROM User u ORDER BY id")
|
||||
Page<User> findAllUsersWithPagination(Pageable pageable);
|
||||
|
||||
@Query(value = "SELECT * FROM Users ORDER BY id \n-- #pageable\n", countQuery = "SELECT count(*) FROM Users", nativeQuery = true)
|
||||
Page<User> findAllUsersWithPaginationNative(Pageable pageable);
|
||||
|
||||
@Modifying
|
||||
@Query("update User u set u.status = :status where u.name = :name")
|
||||
int updateUserSetStatusForName(@Param("status") Integer status, @Param("name") String name);
|
||||
|
||||
@Modifying
|
||||
@Query(value = "UPDATE Users u SET u.status = ? WHERE u.name = ?", nativeQuery = true)
|
||||
int updateUserSetStatusForNameNative(Integer status, String name);
|
||||
|
||||
}
|
||||
@@ -2,4 +2,10 @@ management.health.probes.enabled=true
|
||||
management.endpoint.health.show-details=always
|
||||
management.endpoint.health.status.http-mapping.down=500
|
||||
management.endpoint.health.status.http-mapping.out_of_service=503
|
||||
management.endpoint.health.status.http-mapping.warning=500
|
||||
management.endpoint.health.status.http-mapping.warning=500
|
||||
|
||||
## Configuring info endpoint
|
||||
info.app.name=Spring Sample Application
|
||||
info.app.description=This is my first spring boot application G1
|
||||
info.app.version=1.0.0
|
||||
info.java-vendor = ${java.specification.vendor}
|
||||
@@ -26,6 +26,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
|
||||
@@ -26,6 +26,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.conditionalonproperty;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class NotificationApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(NotificationApplication.class, args);
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.conditionalonproperty.config;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.baeldung.conditionalonproperty.service.EmailNotification;
|
||||
import com.baeldung.conditionalonproperty.service.NotificationSender;
|
||||
import com.baeldung.conditionalonproperty.service.SmsNotification;
|
||||
|
||||
@Configuration
|
||||
public class NotificationConfig {
|
||||
|
||||
@Bean(name = "emailNotification")
|
||||
@ConditionalOnProperty(prefix = "notification", name = "service", havingValue = "email")
|
||||
public NotificationSender notificationSender() {
|
||||
return new EmailNotification();
|
||||
}
|
||||
|
||||
@Bean(name = "smsNotification")
|
||||
@ConditionalOnProperty(prefix = "notification", name = "service", havingValue = "sms")
|
||||
public NotificationSender notificationSender2() {
|
||||
return new SmsNotification();
|
||||
}
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.conditionalonproperty.service;
|
||||
|
||||
public class EmailNotification implements NotificationSender {
|
||||
|
||||
@Override
|
||||
public String send(String message) {
|
||||
return "Email Notification: " + message;
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.conditionalonproperty.service;
|
||||
|
||||
public interface NotificationSender {
|
||||
String send(String message);
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.conditionalonproperty.service;
|
||||
|
||||
public class SmsNotification implements NotificationSender {
|
||||
|
||||
@Override
|
||||
public String send(String message) {
|
||||
return "SMS notification: " + message;
|
||||
}
|
||||
|
||||
}
|
||||
+2
@@ -1,2 +1,4 @@
|
||||
spring.jpa.show-sql=true
|
||||
spring.jpa.hibernate.ddl-auto = update
|
||||
|
||||
notification.service=email
|
||||
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.conditionalonproperty;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
import com.baeldung.conditionalonproperty.config.NotificationConfig;
|
||||
import com.baeldung.conditionalonproperty.service.EmailNotification;
|
||||
import com.baeldung.conditionalonproperty.service.NotificationSender;
|
||||
|
||||
public class NotificationUnitTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
public void whenValueSetToEmail_thenCreateEmailNotification() {
|
||||
this.contextRunner.withPropertyValues("notification.service=email")
|
||||
.withUserConfiguration(NotificationConfig.class)
|
||||
.run(context -> {
|
||||
assertThat(context).hasBean("emailNotification");
|
||||
NotificationSender notificationSender = context.getBean(EmailNotification.class);
|
||||
assertThat(notificationSender.send("Hello From Baeldung!")).isEqualTo("Email Notification: Hello From Baeldung!");
|
||||
assertThat(context).doesNotHaveBean("smsNotification");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,9 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-1</artifactId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-1</relativePath>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@@ -38,12 +38,10 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>${spring-boot-starter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>${spring-boot-starter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
@@ -55,7 +53,6 @@
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring-boot-starter.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
@@ -68,8 +65,7 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<camel.version>2.19.1</camel.version>
|
||||
<spring-boot-starter.version>1.5.4.RELEASE</spring-boot-starter.version>
|
||||
<camel.version>3.0.0-M4</camel.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
+29
-27
@@ -12,33 +12,36 @@ import org.apache.camel.model.rest.RestBindingMode;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.aop.AopAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration;
|
||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackages="com.baeldung.camel")
|
||||
public class Application{
|
||||
@SpringBootApplication(exclude = { WebSocketServletAutoConfiguration.class, AopAutoConfiguration.class, OAuth2ResourceServerAutoConfiguration.class, EmbeddedWebServerFactoryCustomizerAutoConfiguration.class })
|
||||
@ComponentScan(basePackages = "com.baeldung.camel")
|
||||
public class Application {
|
||||
|
||||
@Value("${server.port}")
|
||||
String serverPort;
|
||||
|
||||
|
||||
@Value("${baeldung.api.path}")
|
||||
String contextPath;
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
ServletRegistrationBean servletRegistrationBean() {
|
||||
ServletRegistrationBean servlet = new ServletRegistrationBean(new CamelHttpTransportServlet(), contextPath+"/*");
|
||||
ServletRegistrationBean servlet = new ServletRegistrationBean(new CamelHttpTransportServlet(), contextPath + "/*");
|
||||
servlet.setName("CamelServlet");
|
||||
return servlet;
|
||||
}
|
||||
|
||||
|
||||
@Component
|
||||
class RestApi extends RouteBuilder {
|
||||
|
||||
@@ -47,7 +50,6 @@ public class Application{
|
||||
|
||||
CamelContext context = new DefaultCamelContext();
|
||||
|
||||
|
||||
// http://localhost:8080/camel/api-doc
|
||||
restConfiguration().contextPath(contextPath) //
|
||||
.port(serverPort)
|
||||
@@ -60,43 +62,43 @@ public class Application{
|
||||
.component("servlet")
|
||||
.bindingMode(RestBindingMode.json)
|
||||
.dataFormatProperty("prettyPrint", "true");
|
||||
/**
|
||||
The Rest DSL supports automatic binding json/xml contents to/from
|
||||
POJOs using Camels Data Format.
|
||||
By default the binding mode is off, meaning there is no automatic
|
||||
binding happening for incoming and outgoing messages.
|
||||
You may want to use binding if you develop POJOs that maps to
|
||||
your REST services request and response types.
|
||||
*/
|
||||
|
||||
/**
|
||||
The Rest DSL supports automatic binding json/xml contents to/from
|
||||
POJOs using Camels Data Format.
|
||||
By default the binding mode is off, meaning there is no automatic
|
||||
binding happening for incoming and outgoing messages.
|
||||
You may want to use binding if you develop POJOs that maps to
|
||||
your REST services request and response types.
|
||||
*/
|
||||
|
||||
rest("/api/").description("Teste REST Service")
|
||||
.id("api-route")
|
||||
.post("/bean")
|
||||
.produces(MediaType.APPLICATION_JSON)
|
||||
.consumes(MediaType.APPLICATION_JSON)
|
||||
// .get("/hello/{place}")
|
||||
// .get("/hello/{place}")
|
||||
.bindingMode(RestBindingMode.auto)
|
||||
.type(MyBean.class)
|
||||
.enableCORS(true)
|
||||
// .outType(OutBean.class)
|
||||
// .outType(OutBean.class)
|
||||
|
||||
.to("direct:remoteService");
|
||||
|
||||
|
||||
from("direct:remoteService")
|
||||
.routeId("direct-route")
|
||||
|
||||
from("direct:remoteService").routeId("direct-route")
|
||||
.tracing()
|
||||
.log(">>> ${body.id}")
|
||||
.log(">>> ${body.name}")
|
||||
// .transform().simple("blue ${in.body.name}")
|
||||
// .transform().simple("blue ${in.body.name}")
|
||||
.process(new Processor() {
|
||||
@Override
|
||||
public void process(Exchange exchange) throws Exception {
|
||||
MyBean bodyIn = (MyBean) exchange.getIn().getBody();
|
||||
|
||||
MyBean bodyIn = (MyBean) exchange.getIn()
|
||||
.getBody();
|
||||
|
||||
ExampleServices.example(bodyIn);
|
||||
|
||||
exchange.getIn().setBody(bodyIn);
|
||||
exchange.getIn()
|
||||
.setBody(bodyIn);
|
||||
}
|
||||
})
|
||||
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(201));
|
||||
|
||||
@@ -12,4 +12,6 @@ management.port=8081
|
||||
|
||||
# disable all management enpoints except health
|
||||
endpoints.enabled = true
|
||||
endpoints.health.enabled = true
|
||||
endpoints.health.enabled = true
|
||||
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
@@ -20,6 +20,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
|
||||
+1
@@ -9,3 +9,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
|
||||
- [Spring Component Scanning](https://www.baeldung.com/spring-component-scanning)
|
||||
- [Spring @ComponentScan – Filter Types](https://www.baeldung.com/spring-componentscan-filter-type)
|
||||
- [How to Get All Spring-Managed Beans?](https://www.baeldung.com/spring-show-all-beans)
|
||||
@@ -27,6 +27,12 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
## Spring Boot Groovy
|
||||
|
||||
This module contains articles about Spring with Groovy
|
||||
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Building a Simple Web Application with Spring Boot and Groovy](https://www.baeldung.com/spring-boot-groovy-web-app)
|
||||
- [Groovy Bean Definitions](https://www.baeldung.com/spring-groovy-beans)
|
||||
@@ -0,0 +1,78 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.app</groupId>
|
||||
<artifactId>spring-boot-groovy</artifactId>
|
||||
<name>spring-boot-groovy</name>
|
||||
<packaging>war</packaging>
|
||||
<description>Spring Boot Todo Application with Groovy</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.gmavenplus</groupId>
|
||||
<artifactId>gmavenplus-plugin</artifactId>
|
||||
<version>1.9.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>addSources</goal>
|
||||
<goal>addTestSources</goal>
|
||||
<goal>generateStubs</goal>
|
||||
<goal>compile</goal>
|
||||
<goal>generateTestStubs</goal>
|
||||
<goal>compileTests</goal>
|
||||
<goal>removeStubs</goal>
|
||||
<goal>removeTestStubs</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<start-class>com.baeldung.springwithgroovy.SpringBootGroovyApplication</start-class>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.springwithgroovy
|
||||
|
||||
import org.springframework.boot.SpringApplication
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication
|
||||
|
||||
import com.baeldung.springwithgroovy.SpringBootGroovyApplication
|
||||
|
||||
@SpringBootApplication
|
||||
class SpringBootGroovyApplication {
|
||||
static void main(String[] args) {
|
||||
SpringApplication.run SpringBootGroovyApplication, args
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.springwithgroovy.controller
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.web.bind.annotation.DeleteMapping
|
||||
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.PutMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RequestMethod
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
import com.baeldung.springwithgroovy.entity.Todo
|
||||
import com.baeldung.springwithgroovy.service.TodoService
|
||||
|
||||
@RestController
|
||||
@RequestMapping('todo')
|
||||
public class TodoController {
|
||||
|
||||
@Autowired
|
||||
TodoService todoService
|
||||
|
||||
@GetMapping
|
||||
List<Todo> getAllTodoList(){
|
||||
todoService.findAll()
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
Todo saveTodo(@RequestBody Todo todo){
|
||||
todoService.saveTodo todo
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
Todo updateTodo(@RequestBody Todo todo){
|
||||
todoService.updateTodo todo
|
||||
}
|
||||
|
||||
@DeleteMapping('/{todoId}')
|
||||
deleteTodo(@PathVariable Integer todoId){
|
||||
todoService.deleteTodo todoId
|
||||
}
|
||||
|
||||
@GetMapping('/{todoId}')
|
||||
Todo getTodoById(@PathVariable Integer todoId){
|
||||
todoService.findById todoId
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.springwithgroovy.entity
|
||||
|
||||
import javax.persistence.Column
|
||||
import javax.persistence.Entity
|
||||
import javax.persistence.GeneratedValue
|
||||
import javax.persistence.GenerationType
|
||||
import javax.persistence.Id
|
||||
import javax.persistence.Table
|
||||
|
||||
@Entity
|
||||
@Table(name = 'todo')
|
||||
class Todo {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
Integer id
|
||||
|
||||
@Column
|
||||
String task
|
||||
|
||||
@Column
|
||||
Boolean isCompleted
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.springwithgroovy.repository
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
import org.springframework.stereotype.Repository
|
||||
|
||||
import com.baeldung.springwithgroovy.entity.Todo
|
||||
|
||||
@Repository
|
||||
interface TodoRepository extends JpaRepository<Todo, Integer> {}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.springwithgroovy.service
|
||||
|
||||
import com.baeldung.springwithgroovy.entity.Todo
|
||||
|
||||
interface TodoService {
|
||||
|
||||
List<Todo> findAll()
|
||||
|
||||
Todo findById(Integer todoId)
|
||||
|
||||
Todo saveTodo(Todo todo)
|
||||
|
||||
Todo updateTodo(Todo todo)
|
||||
|
||||
Todo deleteTodo(Integer todoId)
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.springwithgroovy.service.impl
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
import com.baeldung.springwithgroovy.entity.Todo
|
||||
import com.baeldung.springwithgroovy.repository.TodoRepository
|
||||
import com.baeldung.springwithgroovy.service.TodoService
|
||||
|
||||
@Service
|
||||
class TodoServiceImpl implements TodoService {
|
||||
|
||||
@Autowired
|
||||
TodoRepository todoRepository
|
||||
|
||||
@Override
|
||||
List<Todo> findAll() {
|
||||
todoRepository.findAll()
|
||||
}
|
||||
|
||||
@Override
|
||||
Todo findById(Integer todoId) {
|
||||
todoRepository.findById todoId get()
|
||||
}
|
||||
|
||||
@Override
|
||||
Todo saveTodo(Todo todo){
|
||||
todoRepository.save todo
|
||||
}
|
||||
|
||||
@Override
|
||||
Todo updateTodo(Todo todo){
|
||||
todoRepository.save todo
|
||||
}
|
||||
|
||||
@Override
|
||||
Todo deleteTodo(Integer todoId){
|
||||
todoRepository.deleteById todoId
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.groovyconfig;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BandsBean {
|
||||
|
||||
private List<String> bandsList = new ArrayList<>();
|
||||
|
||||
public List<String> getBandsList() {
|
||||
return bandsList;
|
||||
}
|
||||
|
||||
public void setBandsList(List<String> bandsList) {
|
||||
this.bandsList = bandsList;
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.groovyconfig;
|
||||
|
||||
beans {
|
||||
javaPesronBean(JavaPersonBean) {
|
||||
firstName = 'John'
|
||||
lastName = 'Doe'
|
||||
age ='32'
|
||||
eyesColor = 'blue'
|
||||
hairColor='black'
|
||||
}
|
||||
|
||||
bandsBean(BandsBean) { bean->
|
||||
bean.scope = "singleton"
|
||||
bandsList=['Nirvana', 'Pearl Jam', 'Foo Fighters']
|
||||
}
|
||||
|
||||
registerAlias("bandsBean","bands")
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.groovyconfig;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class JavaBeanConfig {
|
||||
|
||||
@Bean
|
||||
public JavaPersonBean javaPerson() {
|
||||
JavaPersonBean jPerson = new JavaPersonBean();
|
||||
jPerson.setFirstName("John");
|
||||
jPerson.setLastName("Doe");
|
||||
jPerson.setAge("31");
|
||||
jPerson.setEyesColor("green");
|
||||
jPerson.setHairColor("blond");
|
||||
|
||||
return jPerson;
|
||||
}
|
||||
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.groovyconfig;
|
||||
|
||||
public class JavaPersonBean {
|
||||
|
||||
public String jj;
|
||||
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private String age;
|
||||
|
||||
private String eyesColor;
|
||||
|
||||
private String hairColor;
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(String age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getEyesColor() {
|
||||
return eyesColor;
|
||||
}
|
||||
|
||||
public void setEyesColor(String eyesColor) {
|
||||
this.eyesColor = eyesColor;
|
||||
}
|
||||
|
||||
public String getHairColor() {
|
||||
return hairColor;
|
||||
}
|
||||
|
||||
public void setHairColor(String hairColor) {
|
||||
this.hairColor = hairColor;
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.spring_groovy;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
System.out.println( "Hello World!" );
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.spring_groovy;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class TestConfig {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
spring.datasource.url=jdbc:h2:mem:todo
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=sa
|
||||
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||
@@ -0,0 +1,5 @@
|
||||
beans{
|
||||
testString String, 'test'
|
||||
testNum int, 100
|
||||
testObj(i:101,s:'objVal')
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
<bean id="JavaPersonBean" class="com.baeldung.groovyconfig.JavaPersonBean">
|
||||
<property name="firstName" value="John" />
|
||||
<property name="LastName" value="Doe" />
|
||||
<property name="age" value="30" />
|
||||
<property name="eyesColor" value="brown" />
|
||||
<property name="hairColor" value="brown" />
|
||||
</bean>
|
||||
</beans>
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
package com.baeldung.springwithgroovy
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue
|
||||
|
||||
import org.junit.BeforeClass
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.springframework.boot.test.context.SpringBootTest
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.test.context.event.annotation.BeforeTestClass
|
||||
import org.springframework.test.context.junit4.SpringRunner
|
||||
|
||||
import com.baeldung.springwithgroovy.entity.Todo
|
||||
|
||||
import io.restassured.RestAssured
|
||||
import io.restassured.response.Response
|
||||
|
||||
// This test requires the com.baeldung.springwithgroovy.SpringBootGroovyApplication to be up
|
||||
// For that, run the maven build - spring-boot:run on the module
|
||||
|
||||
class TodoAppLiveTest {
|
||||
static API_ROOT = 'http://localhost:8080/todo'
|
||||
static readingTodoId
|
||||
static writingTodoId
|
||||
|
||||
@BeforeClass
|
||||
static void populateDummyData() {
|
||||
Todo readingTodo = new Todo(task: 'Reading', isCompleted: false)
|
||||
Todo writingTodo = new Todo(task: 'Writing', isCompleted: false)
|
||||
|
||||
final Response readingResponse =
|
||||
RestAssured.given()
|
||||
.contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||
.body(readingTodo).post(API_ROOT)
|
||||
|
||||
Todo cookingTodoResponse = readingResponse.as Todo.class
|
||||
readingTodoId = cookingTodoResponse.getId()
|
||||
|
||||
final Response writingResponse =
|
||||
RestAssured.given()
|
||||
.contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||
.body(writingTodo).post(API_ROOT)
|
||||
|
||||
Todo writingTodoResponse = writingResponse.as Todo.class
|
||||
writingTodoId = writingTodoResponse.getId()
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenGetAllTodoList_thenOk(){
|
||||
final Response response = RestAssured.get(API_ROOT)
|
||||
|
||||
assertEquals HttpStatus.OK.value(),response.getStatusCode()
|
||||
assertTrue response.as(List.class).size() > 0
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenGetTodoById_thenOk(){
|
||||
final Response response =
|
||||
RestAssured.get("$API_ROOT/$readingTodoId")
|
||||
|
||||
assertEquals HttpStatus.OK.value(),response.getStatusCode()
|
||||
Todo todoResponse = response.as Todo.class
|
||||
assertEquals readingTodoId,todoResponse.getId()
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUpdateTodoById_thenOk(){
|
||||
Todo todo = new Todo(id:readingTodoId, isCompleted: true)
|
||||
final Response response =
|
||||
RestAssured.given()
|
||||
.contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||
.body(todo).put(API_ROOT)
|
||||
|
||||
assertEquals HttpStatus.OK.value(),response.getStatusCode()
|
||||
Todo todoResponse = response.as Todo.class
|
||||
assertTrue todoResponse.getIsCompleted()
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenDeleteTodoById_thenOk(){
|
||||
final Response response =
|
||||
RestAssured.given()
|
||||
.delete("$API_ROOT/$writingTodoId")
|
||||
|
||||
assertEquals HttpStatus.OK.value(),response.getStatusCode()
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenSaveTodo_thenOk(){
|
||||
Todo todo = new Todo(task: 'Blogging', isCompleted: false)
|
||||
final Response response =
|
||||
RestAssured.given()
|
||||
.contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||
.body(todo).post(API_ROOT)
|
||||
|
||||
assertEquals HttpStatus.OK.value(),response.getStatusCode()
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.groovyconfig;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.support.GenericGroovyApplicationContext;
|
||||
|
||||
import com.baeldung.groovyconfig.BandsBean;
|
||||
import com.baeldung.groovyconfig.JavaPersonBean;
|
||||
|
||||
public class GroovyConfigurationUnitTest {
|
||||
|
||||
private static final String FILE_NAME = "GroovyBeanConfig.groovy";
|
||||
private static final String FILE_PATH = "src/main/java/com/baeldung/groovyconfig/";
|
||||
|
||||
@Test
|
||||
public void whenGroovyConfig_thenCorrectPerson() throws Exception {
|
||||
|
||||
GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext();
|
||||
ctx.load("file:" + getPathPart() + FILE_NAME);
|
||||
ctx.refresh();
|
||||
|
||||
JavaPersonBean j = ctx.getBean(JavaPersonBean.class);
|
||||
|
||||
assertEquals("32", j.getAge());
|
||||
assertEquals("blue", j.getEyesColor());
|
||||
assertEquals("black", j.getHairColor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGroovyConfig_thenCorrectListLength() throws Exception {
|
||||
|
||||
GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext();
|
||||
ctx.load("file:" + getPathPart() + FILE_NAME);
|
||||
ctx.refresh();
|
||||
|
||||
BandsBean bb = ctx.getBean(BandsBean.class);
|
||||
|
||||
assertEquals(3, bb.getBandsList()
|
||||
.size());
|
||||
}
|
||||
|
||||
private String getPathPart() {
|
||||
String pathPart = new File(".").getAbsolutePath();
|
||||
pathPart = pathPart.replace(".", "");
|
||||
pathPart = pathPart.replace("\\", "/");
|
||||
pathPart = pathPart + FILE_PATH;
|
||||
|
||||
return pathPart;
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.groovyconfig;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
import com.baeldung.groovyconfig.JavaBeanConfig;
|
||||
import com.baeldung.groovyconfig.JavaPersonBean;
|
||||
|
||||
public class JavaConfigurationUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenJavaConfig_thenCorrectPerson() {
|
||||
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(JavaBeanConfig.class);
|
||||
ctx.refresh();
|
||||
|
||||
JavaPersonBean j = ctx.getBean(JavaPersonBean.class);
|
||||
|
||||
assertEquals("31", j.getAge());
|
||||
assertEquals("green", j.getEyesColor());
|
||||
assertEquals("blond", j.getHairColor());
|
||||
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.groovyconfig;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import com.baeldung.groovyconfig.JavaPersonBean;
|
||||
|
||||
public class XmlConfigurationUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenXmlConfig_thenCorrectPerson() {
|
||||
final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("xml-bean-config.xml");
|
||||
|
||||
JavaPersonBean j = (JavaPersonBean) applicationContext.getBean("JavaPersonBean");
|
||||
|
||||
assertEquals("30", j.getAge());
|
||||
assertEquals("brown", j.getEyesColor());
|
||||
assertEquals("brown", j.getHairColor());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.spring_groovy;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppUnitTest
|
||||
extends TestCase
|
||||
{
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppUnitTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppUnitTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rigourous Test :-)
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.keycloak;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.Map;
|
||||
|
||||
import org.keycloak.KeycloakPrincipal;
|
||||
import org.keycloak.KeycloakSecurityContext;
|
||||
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
|
||||
import org.keycloak.representations.IDToken;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
@Controller
|
||||
public class CustomUserAttrController {
|
||||
|
||||
@GetMapping(path = "/users")
|
||||
public String getUserInfo(Model model) {
|
||||
|
||||
KeycloakAuthenticationToken authentication = (KeycloakAuthenticationToken) SecurityContextHolder.getContext()
|
||||
.getAuthentication();
|
||||
|
||||
final Principal principal = (Principal) authentication.getPrincipal();
|
||||
|
||||
String dob = "";
|
||||
|
||||
if (principal instanceof KeycloakPrincipal) {
|
||||
|
||||
KeycloakPrincipal<KeycloakSecurityContext> kPrincipal = (KeycloakPrincipal<KeycloakSecurityContext>) principal;
|
||||
IDToken token = kPrincipal.getKeycloakSecurityContext()
|
||||
.getIdToken();
|
||||
|
||||
Map<String, Object> customClaims = token.getOtherClaims();
|
||||
|
||||
if (customClaims.containsKey("DOB")) {
|
||||
dob = String.valueOf(customClaims.get("DOB"));
|
||||
}
|
||||
}
|
||||
|
||||
model.addAttribute("username", principal.getName());
|
||||
model.addAttribute("dob", dob);
|
||||
return "userInfo";
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -44,7 +44,7 @@ class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
super.configure(http);
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/customers*")
|
||||
.antMatchers("/customers*", "/users*")
|
||||
.hasRole("user")
|
||||
.anyRequest()
|
||||
.permitAll();
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
<!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>
|
||||
<h3>
|
||||
Your Date of Birth as per our records is <span th:text="${dob}" />.
|
||||
</h3>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -12,3 +12,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
- [Generating Barcodes and QR Codes in Java](https://www.baeldung.com/java-generating-barcodes-qr-codes)
|
||||
- [Rate Limiting a Spring API Using Bucket4j](https://www.baeldung.com/spring-bucket4j)
|
||||
- [Spring Boot and Caffeine Cache](https://www.baeldung.com/spring-boot-caffeine-cache)
|
||||
- [Spring Boot and Togglz Aspect](https://www.baeldung.com/spring-togglz)
|
||||
- [Getting Started with GraphQL and Spring Boot](https://www.baeldung.com/spring-graphql)
|
||||
- [An Introduction to Kong](https://www.baeldung.com/kong)
|
||||
|
||||
@@ -37,6 +37,36 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- togglez -->
|
||||
<dependency>
|
||||
<groupId>org.togglz</groupId>
|
||||
<artifactId>togglz-spring-boot-starter</artifactId>
|
||||
<version>${togglz.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.togglz</groupId>
|
||||
<artifactId>togglz-spring-security</artifactId>
|
||||
<version>${togglz.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- graphql -->
|
||||
<dependency>
|
||||
<groupId>com.graphql-java</groupId>
|
||||
<artifactId>graphql-spring-boot-starter</artifactId>
|
||||
<version>${graphql-spring-boot-starter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.graphql-java</groupId>
|
||||
<artifactId>graphql-java-tools</artifactId>
|
||||
<version>${graphql-java-tools.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.graphql-java</groupId>
|
||||
<artifactId>graphiql-spring-boot-starter</artifactId>
|
||||
<version>${graphql-spring-boot-starter.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Problem Spring Web -->
|
||||
<dependency>
|
||||
@@ -216,7 +246,6 @@
|
||||
<rome.version>1.9.0</rome.version>
|
||||
<chaos.monkey.version>2.0.0</chaos.monkey.version>
|
||||
<graphql-spring-boot-starter.version>5.0.2</graphql-spring-boot-starter.version>
|
||||
<graphiql-spring-boot-starter.version>5.0.2</graphiql-spring-boot-starter.version>
|
||||
<graphql-java-tools.version>5.2.4</graphql-java-tools.version>
|
||||
<guava.version>18.0</guava.version>
|
||||
<git-commit-id-plugin.version>2.2.4</git-commit-id-plugin.version>
|
||||
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.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);
|
||||
}
|
||||
|
||||
}
|
||||
+37
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,6 +23,11 @@
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.asyncvsflux;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class AsyncController {
|
||||
|
||||
@GetMapping("/async_result")
|
||||
@Async
|
||||
public CompletableFuture<String> getResultAsyc(HttpServletRequest request) {
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return CompletableFuture.completedFuture("Result is ready!");
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.asyncvsflux;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class AsyncFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
||||
try {
|
||||
Thread.sleep(200);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
filterChain.doFilter(servletRequest, servletResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.asyncvsflux;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableAsync
|
||||
public class AsyncVsWebFluxApp {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AsyncVsWebFluxApp.class, args);
|
||||
}
|
||||
}
|
||||
@@ -9,4 +9,6 @@ This module contains articles about Spring Web MVC in Spring Boot projects.
|
||||
- [A Controller, Service and DAO Example with Spring Boot and JSF](https://www.baeldung.com/jsf-spring-boot-controller-service-dao)
|
||||
- [Setting Up Swagger 2 with a Spring REST API](https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api)
|
||||
- [Using Spring ResponseEntity to Manipulate the HTTP Response](https://www.baeldung.com/spring-response-entity)
|
||||
- [The @ServletComponentScan Annotation in Spring Boot](https://www.baeldung.com/spring-servletcomponentscan)
|
||||
- [Guide to Internationalization in Spring Boot](https://www.baeldung.com/spring-boot-internationalization)
|
||||
- More articles: [[next -->]](/spring-boot-modules/spring-boot-mvc-2)
|
||||
|
||||
@@ -17,15 +17,6 @@
|
||||
<name>spring-boot-mvc</name>
|
||||
<description>Module For Spring Boot MVC</description>
|
||||
|
||||
<repositories>
|
||||
<!-- Snapshot repository location -->
|
||||
<repository>
|
||||
<id>jcenter-release</id>
|
||||
<name>jcenter</name>
|
||||
<url>http://oss.jfrog.org/artifactory/oss-release-local/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
@@ -49,6 +40,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-rest</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
@@ -99,24 +94,7 @@
|
||||
<!-- Spring Fox 2 -->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-data-rest</artifactId>
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-bean-validators</artifactId>
|
||||
<artifactId>springfox-boot-starter</artifactId>
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user