JAVA-11420: Dissolve spring-boot-1 and spring-boot-2 inside (#12122)
spring-boot-modules
This commit is contained in:
@@ -6,3 +6,5 @@
|
||||
- [Creating a Read-Only Repository with Spring Data](https://www.baeldung.com/spring-data-read-only-repository)
|
||||
- [Using JaVers for Data Model Auditing in Spring Data](https://www.baeldung.com/spring-data-javers-audit)
|
||||
- [BootstrapMode for JPA Repositories](https://github.com/eugenp/tutorials/tree/master/spring-boot-modules/spring-boot-data-2)
|
||||
- [Dynamic DTO Validation Config Retrieved from the Database](https://www.baeldung.com/spring-dynamic-dto-validation)
|
||||
|
||||
|
||||
@@ -16,6 +16,14 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</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>
|
||||
@@ -31,7 +39,7 @@
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<properties>
|
||||
<javers.version>6.5.3</javers.version>
|
||||
</properties>
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.dynamicvalidation;
|
||||
|
||||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
|
||||
import static java.lang.annotation.ElementType.CONSTRUCTOR;
|
||||
import static java.lang.annotation.ElementType.FIELD;
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.ElementType.PARAMETER;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
|
||||
@Constraint(validatedBy = { ContactInfoValidator.class })
|
||||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ContactInfo {
|
||||
String message() default "Invalid value";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.dynamicvalidation;
|
||||
|
||||
import com.baeldung.dynamicvalidation.dao.ContactInfoExpressionRepository;
|
||||
import com.baeldung.dynamicvalidation.model.ContactInfoExpression;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.thymeleaf.util.StringUtils;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ContactInfoValidator implements ConstraintValidator<ContactInfo, String> {
|
||||
|
||||
private static final Logger LOG = LogManager.getLogger(ContactInfoValidator.class);
|
||||
|
||||
@Autowired
|
||||
private ContactInfoExpressionRepository expressionRepository;
|
||||
|
||||
@Value("${contactInfoType}")
|
||||
String expressionType;
|
||||
|
||||
private String pattern;
|
||||
|
||||
@Override
|
||||
public void initialize(final ContactInfo contactInfo) {
|
||||
if (StringUtils.isEmptyOrWhitespace(expressionType)) {
|
||||
LOG.error("Contact info type missing!");
|
||||
} else {
|
||||
pattern = expressionRepository.findById(expressionType).map(ContactInfoExpression::getPattern).orElse("");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(final String value, final ConstraintValidatorContext context) {
|
||||
if (!StringUtils.isEmptyOrWhitespace(pattern)) {
|
||||
return Pattern.matches(pattern, value);
|
||||
}
|
||||
LOG.error("Contact info pattern missing!");
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.dynamicvalidation;
|
||||
|
||||
import javax.annotation.security.RolesAllowed;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DynamicValidationApp {
|
||||
@RolesAllowed("*")
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DynamicValidationApp.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.dynamicvalidation.config;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import com.baeldung.dynamicvalidation.model.Customer;
|
||||
|
||||
@Controller
|
||||
public class CustomerController {
|
||||
|
||||
@GetMapping("/customer")
|
||||
public String getCustomerPage(Model model) {
|
||||
return "customer";
|
||||
}
|
||||
|
||||
@PostMapping("/customer")
|
||||
public String validateCustomer(@Valid final Customer customer, final BindingResult result, final Model model) {
|
||||
if (result.hasErrors()) {
|
||||
model.addAttribute("message", "The information is invalid!");
|
||||
} else {
|
||||
model.addAttribute("message", "The information is valid!");
|
||||
}
|
||||
return "customer";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.dynamicvalidation.config;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
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.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
@EnableJpaRepositories("com.baeldung.dynamicvalidation.dao")
|
||||
@EntityScan("com.baeldung.dynamicvalidation.model")
|
||||
@Configuration
|
||||
public class PersistenceConfig {
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
|
||||
EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.H2).addScript("schema-expressions.sql").addScript("data-expressions.sql").build();
|
||||
return db;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.dynamicvalidation.dao;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
import com.baeldung.dynamicvalidation.model.ContactInfoExpression;
|
||||
|
||||
public interface ContactInfoExpressionRepository extends Repository<ContactInfoExpression, String> {
|
||||
Optional<ContactInfoExpression> findById(String id);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.dynamicvalidation.model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class ContactInfoExpression {
|
||||
|
||||
@Id
|
||||
@Column(name = "expression_type")
|
||||
private String type;
|
||||
private String pattern;
|
||||
|
||||
public ContactInfoExpression() {
|
||||
|
||||
}
|
||||
|
||||
public ContactInfoExpression(final String type, final String pattern) {
|
||||
this.type = type;
|
||||
this.pattern = pattern;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(final String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getPattern() {
|
||||
return pattern;
|
||||
}
|
||||
|
||||
public void setPattern(final String pattern) {
|
||||
this.pattern = pattern;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.dynamicvalidation.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import com.baeldung.dynamicvalidation.ContactInfo;
|
||||
|
||||
@Entity
|
||||
public class Customer {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
@ContactInfo
|
||||
@NotNull
|
||||
private String contactInfo;
|
||||
|
||||
public Customer() {
|
||||
}
|
||||
|
||||
public Customer(final long id, final String contactInfo) {
|
||||
this.id = id;
|
||||
this.contactInfo = contactInfo;
|
||||
}
|
||||
|
||||
public String getContactInfo() {
|
||||
return contactInfo;
|
||||
}
|
||||
|
||||
public void setContactInfo(final String contactInfo) {
|
||||
this.contactInfo = contactInfo;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
insert into contact_info_expression values ('email',
|
||||
'[a-z0-9!#$%&*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?')
|
||||
insert into contact_info_expression values ('phone',
|
||||
'^([0-9]( |-)?)?(\(?[0-9]{3}\)?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[a-zA-Z0-9]{7})$')
|
||||
insert into contact_info_expression values ('website',
|
||||
'^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$')
|
||||
@@ -0,0 +1,5 @@
|
||||
CREATE TABLE contact_info_expression(
|
||||
expression_type varchar(50) not null,
|
||||
pattern varchar(500) not null,
|
||||
PRIMARY KEY ( expression_type )
|
||||
);
|
||||
@@ -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>
|
||||
@@ -0,0 +1,20 @@
|
||||
spring.mail.host=localhost
|
||||
spring.mail.port=8025
|
||||
spring.mail.properties.mail.smtp.auth=false
|
||||
|
||||
# spring.datasource.x
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=sa
|
||||
|
||||
# hibernate.X
|
||||
spring.jpa.hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
spring.jpa.hibernate.show_sql=true
|
||||
spring.jpa.hibernate.hbm2ddl.auto=create-drop
|
||||
spring.jpa.hibernate.cache.use_second_level_cache=true
|
||||
spring.jpa.hibernate.cache.use_query_cache=true
|
||||
spring.jpa.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
|
||||
|
||||
management.security.enabled=false
|
||||
Reference in New Issue
Block a user