Spring Tool Suite/Gradle/MapStruct - Cannot find implementation for mapper - spring-boot

I have searched StackOverflow for an answer to my question. While I have found many offered solutions, none work in my situation.
I have to use MapStruct for a project at work, so I am trying to learn it through Tutorialspoint. However, instead of using Maven as the tutorial suggests, I am using Gradle, which I have to do because my company uses Gradle instead of Maven. I have seen answers in StackOverflow that moving the project to Maven gets it to work, but that is not an option in my case.
I am using version 1.4.2.Final instead of 1.5.0.RC1 because I had a different issue when using the latter that resolved by switching to the former.
Here are the parts of my code:
build.gradle
plugins {
id 'org.springframework.boot' version '2.6.5'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.tutorialspoint'
version = '2.4.2'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation group: 'org.mapstruct', name: 'mapstruct', version: '1.4.2.Final'
implementation 'org.mapstruct:mapstruct:1.4.2.Final'
annotationProcessor "org.mapstruct:mapstruct-processor:1.4.2.Final"
compileOnly 'org.mapstruct:mapstruct-processor:1.4.2.Final'
}
tasks.named('test') {
useJUnitPlatform()
}
compileJava {
options.compilerArgs += [
'-Amapstruct.suppressGeneratorTimestamp=true',
'-Amapstruct.suppressGeneratorVersionInfoComment=true',
'-Amapstruct.verbose=true'
]
}
Student.java
package com.tutorialspoint.mapstructproject;
public class Student {
private int id;
private String name;
private String className;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
StudentEntity.java
package com.tutorialspoint.mapstructproject;
public class StudentEntity {
private int id;
private String name;
private String classVal;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassVal() {
return classVal;
}
public void setClassVal(String classVal) {
this.classVal = classVal;
}
}
StudentMapper.java
package com.tutorialspoint.mapstructproject;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
#Mapper(componentModel="spring")
public interface StudentMapper {
#Mapping(target="className", source="classVal")
Student getModelFromEntity(StudentEntity student);
#Mapping(target="classVal", source="className")
StudentEntity getEntityFromModel(Student student);
}
StudentMapperTest.java
package com.tutorialspoint.mapstructproject;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
import com.tutorialspoint.mapstructproject.StudentEntity;
import com.tutorialspoint.mapstructproject.StudentMapper;
import com.tutorialspoint.mapstructproject.Student;
public class StudentMapperTest {
private StudentMapper studentMapper = Mappers.getMapper(StudentMapper.class);
#Test
public void testEntityToModel() {
StudentEntity entity = new StudentEntity();
entity.setClassVal("X");
entity.setName("John");
entity.setId(1);
Student model = studentMapper.getModelFromEntity(entity);
assertEquals(entity.getClassVal(), model.getClassName());
assertEquals(entity.getName(), model.getName());
assertEquals(entity.getId(), model.getId());
}
#Test
public void testModelToEntity() {
Student model = new Student();
model.setId(1);
model.setName("John");
model.setClassName("X");
StudentEntity entity = studentMapper.getEntityFromModel(model);
assertEquals(entity.getClassVal(), model.getClassName());
assertEquals(entity.getName(), model.getName());
assertEquals(entity.getId(), model.getId());
}
}
Here is the stack trace:
java.lang.RuntimeException: java.lang.ClassNotFoundException: Cannot find implementation for com.tutorialspoint.mapstructproject.StudentMapper
at org.mapstruct.factory.Mappers.getMapper(Mappers.java:61)
at com.tutorialspoint.mapstructproject.StudentMapperTest.<init>(StudentMapperTest.java:11)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.junit.platform.commons.util.ReflectionUtils.newInstance(ReflectionUtils.java:550)
at org.junit.jupiter.engine.execution.ConstructorInvocation.proceed(ConstructorInvocation.java:56)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.api.extension.InvocationInterceptor.interceptTestClassConstructor(InvocationInterceptor.java:73)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:77)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeTestClassConstructor(ClassBasedTestDescriptor.java:355)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateTestClass(ClassBasedTestDescriptor.java:302)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.instantiateTestClass(ClassTestDescriptor.java:79)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateAndPostProcessTestInstance(ClassBasedTestDescriptor.java:280)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$4(ClassBasedTestDescriptor.java:272)
at java.base/java.util.Optional.orElseGet(Optional.java:369)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$5(ClassBasedTestDescriptor.java:271)
at org.junit.jupiter.engine.execution.TestInstancesProvider.getTestInstances(TestInstancesProvider.java:31)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$prepare$0(TestMethodTestDescriptor.java:102)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:101)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:66)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$prepare$2(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.prepare(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:90)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:84)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:768)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: java.lang.ClassNotFoundException: Cannot find implementation for com.tutorialspoint.mapstructproject.StudentMapper
at org.mapstruct.factory.Mappers.getMapper(Mappers.java:75)
at org.mapstruct.factory.Mappers.getMapper(Mappers.java:58)
... 68 more
UPDATE: Someone asked about the build file. I don't see a build file in my project, probably because the build never succeeds due to the error. Here is my file structure:

For me works:
implementation group: 'org.mapstruct', name: 'mapstruct', version: '1.5.3.Final'
implementation group: 'org.mapstruct', name: 'mapstruct-processor', version: '1.5.3.Final'
compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.24'
annotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.24'
annotationProcessor group: 'org.mapstruct', name: 'mapstruct-processor', version: '1.5.3.Final'
Taken from https://github.com/mapstruct/mapstruct-examples/blob/main/mapstruct-lombok/build.gradle
Optionally works without (please confirm it):
annotationProcessor group: 'org.mapstruct', name: 'mapstruct-processor', version: '1.5.3.Final'

Related

Testing Spring Controller class public method

I need to test a public method in spring Controller class. Unfortunately my understanding of Spring is miniscule. All i know is that there are 2 repository classes that are wired to database and the method i want to test retrieves record from both databases like in the example:
#Controller
public class CalculateController {
static double ZERO = 0.00;
static double PROFIT_BOUND = 0.01;
static int DECIMAL_PLACES = 2;
static double MARGIN_PERCENT = 10;
static double PERCENT_DIVIDER = 100;
static boolean renderTable = false;
#Autowired (required = false)
private StateRepository stateRepository;
#Autowired (required = false)
private ProductRepository productRepository;
#Autowired (required = false)
private InternationalTransportCostsRepository internationalTransportCostsRepository;
// some other code unrelated to topic
public Double[] calculateProfitUSA(State state, Product product, Double finalPrice) {
Double[] tab;
if (getTax(state.getId(), product.getCategory()) == ZERO) {
tab = calculateMarginWithoutTaxUSA(state, product, finalPrice);
} else {
tab = calculateMarginWithTaxUSA(state, product, finalPrice);
}
return tab;
}
private Double[] calculateMarginWithTaxUSA(State state, Product product, Double finalPrice) {
Double[] tab = new Double[3];
double profit = calcUSA(state, product, finalPrice);
double estimatedPrice = (product.getCurrentPrice() + state.getTransportCost());
estimatedPrice = estimatedPrice + (estimatedPrice * (getTax(state.getId(), product.getCategory()) / PERCENT_DIVIDER));
double margin = estimatedPrice * MARGIN_PERCENT / PERCENT_DIVIDER;
estimatedPrice = estimatedPrice + (estimatedPrice * MARGIN_PERCENT / PERCENT_DIVIDER);
double currentProfit = calcUSA(state, product, estimatedPrice);
while (currentProfit < margin) {
estimatedPrice += PROFIT_BOUND;
currentProfit = calcUSA(state, product, estimatedPrice);
}
tab[0] = Precision.round(profit, DECIMAL_PLACES);
tab[1] = Precision.round(estimatedPrice, DECIMAL_PLACES);
tab[2] = Precision.round(margin, DECIMAL_PLACES);
return tab;
}
public Double getTax(Long id, String categoryName) {
if (categoryName.equals("Groceries")) {
return stateRepository.findById(id).get().getGroceriesTax();
} else if (categoryName.equals("Prepared-food")) {
return stateRepository.findById(id).get().getPreparedFoodTax();
} else if (categoryName.equals("Prescription-drug")) {
return stateRepository.findById(id).get().getPrescriptionDrugTax();
} else if (categoryName.equals("Non-prescription-drug")) {
return stateRepository.findById(id).get().getNonPrescriptionDrugTax();
} else if (categoryName.equals("Clothing")) {
return stateRepository.findById(id).get().getClothingTax();
} else if (categoryName.equals("Intangibles")) {
return stateRepository.findById(id).get().getIntangiblesTax();
}
return null;
}
I have been trying to search a solution on google for 2 days, I failed. All i managed to come up with is this:
#WebMvcTest(CalculateController.class)
public class CalculateAlgorithmsTests {
#InjectMocks
private CalculateController calc;
#Mock
private StateRepository stateRepository;
#Mock
private ProductRepository productRepository;
private List<State> states;
private List<Product> products;
#Test
public void calculateProfitUSATest(){
states = CSVReader.readStatesFromCSV("csvFiles/states.csv");
products = CSVReader.readProductsFromCSV("csvFiles/products.csv");
productRepository.saveAll(CSVReader.readProductsFromCSV("csvFiles/products.csv"));
stateRepository.saveAll(CSVReader.readStatesFromCSV("csvFiles/states.csv"));
State Connecticut = states.get(5);
Product Apple = products.get(0);
double [] expected = {25,12,32};
Assertions.assertEquals(expected,calc.calculateProfitUSA(Connecticut,Apple,30.23));
}
}
And I get this response from console:
java.util.NoSuchElementException: No value present
at java.base/java.util.Optional.get(Optional.java:141)
at com.taxcalc.controllers.CalculateController.getTax(CalculateController.java:215)
at com.taxcalc.controllers.CalculateController.calculateProfitUSA(CalculateController.java:167)
at com.taxcalc.CalculateAlgorithmsTests.calculateProfitUSATest(CalculateAlgorithmsTests.java:41)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:117)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:184)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:180)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:127)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:68)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
It looks like there is no result for your given id when calling stateRepository.findById(id).
You should mock that in your test by using sth like
State someState = new State()
Mockito.when(stateRepository.findById(someId)).thenReturn(Optional.of(someState))
You need to make sure that someId has the same value as the State you are calling the public method with.
The calls of stateRepository.saveAll() could then be omitted because you would explicitly mock the behaviour rather than using the given data from the CSV.
This is just a suggestion.
Another suggestion is to move this kind of logic into a service. Controllers usually define the HTTP related behaviour rather than haven business logic.

Springboot caching SerializationException using Redis

I'm trying to make a springboot Redis caching demo.
Here is the Redis configuration class:
#Configuration
#EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
#Value("${custom.config.redis.ttl}")
private Integer timeTtl;
#Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
return new RedisCacheManager(
RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
this.getRedisCacheConfigurationWithTtl(timeTtl),
this.getRedisCacheConfigurationMap()
);
}
private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>(10);
redisCacheConfigurationMap
.put("SsoCache", this.getRedisCacheConfigurationWithTtl(24 * 60 * 60));
redisCacheConfigurationMap
.put("BasicDataCache", this.getRedisCacheConfigurationWithTtl(30 * 60));
return redisCacheConfigurationMap;
}
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(
Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.activateDefaultTyping(BasicPolymorphicTypeValidator.builder().build(),
ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration
.defaultCacheConfig();
redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
RedisSerializationContext
.SerializationPair
.fromSerializer(jackson2JsonRedisSerializer)
).entryTtl(Duration.ofSeconds(seconds));
return redisCacheConfiguration;
}
}
redis service:
#Service
#Slf4j
public class RedisService {
private final StringRedisTemplate redisTemplate;
public RedisService(StringRedisTemplate redisTemplate) {
Assert.notNull(redisTemplate, "redisTemplate is required");
this.redisTemplate = redisTemplate;
}
#Cacheable(value = "user", key = "#userRedis.getId()")
public UserRedis saveUser(UserRedis userRedis) {
log.trace("user{} is saved", userRedis);
return userRedis;
}
}
test class:
#SpringBootTest
class RedisServiceTest {
#Autowired
private RedisService redisService;
#Autowired
private StringRedisTemplate stringRedisTemplate;
#Autowired
private RedisTemplate redisTemplate;
#Test
void saveUser() {
UserRedis japhy = UserRedis.builder().id(3L).name("japhy").age(10L).build();
UserRedis userRedis = redisService.saveUser(japhy);
Assertions.assertThat(userRedis).isNotNull();
Assertions.assertThat(userRedis.getId()).isEqualTo(3L);
Assertions.assertThat(userRedis.getName()).isEqualTo("japhy");
Assertions.assertThat(userRedis.getAge()).isEqualTo(10L);
}
}
model class:
#Builder
public class UserRedis implements Serializable {
private static final long serialVersionUID = -1541408164202572383L;
private Long id;
private String name;
private Long age;
public UserRedis() {
}
public UserRedis(Long id, String name, Long age) {
this.id = id;
this.name = name;
this.age = age;
}
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;
}
public Long getAge() {
return age;
}
public void setAge(Long age) {
this.age = age;
}
}
application.yml:
spring:
redis:
database: 0
# Redis服务器地址
host: 39.99.155.113
# Redis服务器连接端口
port: 6379
# Redis服务器连接密码(默认为空)
password:
lettuce:
pool:
max-active: 8
max-wait: -1s
max-idle: 8
min-idle: 0
cache:
redis:
time-to-live: 180s #缓存超时时间ms
cache-null-values: false #是否缓存空值
type: redis
custom:
config:
redis:
ttl: 1800
I can save the modal class to Redis successfully. But when I try to read it from Redis. It throws an exception:
org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Could not resolve type id 'com.japhy.examples.model.UserRedis' as a subtype of `java.lang.Object`: Configured `PolymorphicTypeValidator` (of type `com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator`) denied resolution
at [Source: (byte[])"["com.japhy.examples.model.UserRedis",{"id":3,"name":"japhy","age":10}]"; line: 1, column: 39]; nested exception is com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id 'com.japhy.examples.model.UserRedis' as a subtype of `java.lang.Object`: Configured `PolymorphicTypeValidator` (of type `com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator`) denied resolution
at [Source: (byte[])"["com.japhy.examples.model.UserRedis",{"id":3,"name":"japhy","age":10}]"; line: 1, column: 39]
at org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer.deserialize(Jackson2JsonRedisSerializer.java:75)
at org.springframework.data.redis.serializer.DefaultRedisElementReader.read(DefaultRedisElementReader.java:48)
at org.springframework.data.redis.serializer.RedisSerializationContext$SerializationPair.read(RedisSerializationContext.java:272)
at org.springframework.data.redis.cache.RedisCache.deserializeCacheValue(RedisCache.java:260)
at org.springframework.data.redis.cache.RedisCache.lookup(RedisCache.java:94)
at org.springframework.cache.support.AbstractValueAdaptingCache.get(AbstractValueAdaptingCache.java:58)
at org.springframework.cache.interceptor.AbstractCacheInvoker.doGet(AbstractCacheInvoker.java:73)
at org.springframework.cache.interceptor.CacheAspectSupport.findInCaches(CacheAspectSupport.java:554)
at org.springframework.cache.interceptor.CacheAspectSupport.findCachedItem(CacheAspectSupport.java:519)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:401)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:345)
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
at com.japhy.examples.RedisService$$EnhancerBySpringCGLIB$$e0f4e77a.saveUser(<generated>)
at com.japhy.examples.RedisServiceTest.saveUser(RedisServiceTest.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:125)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:132)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:124)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:74)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:104)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:62)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:43)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:35)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:202)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:198)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:69)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id 'com.japhy.examples.model.UserRedis' as a subtype of `java.lang.Object`: Configured `PolymorphicTypeValidator` (of type `com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator`) denied resolution
at [Source: (byte[])"["com.japhy.examples.model.UserRedis",{"id":3,"name":"japhy","age":10}]"; line: 1, column: 39]
at com.fasterxml.jackson.databind.exc.InvalidTypeIdException.from(InvalidTypeIdException.java:43)
at com.fasterxml.jackson.databind.DeserializationContext.invalidTypeIdException(DeserializationContext.java:1758)
at com.fasterxml.jackson.databind.DatabindContext._throwSubtypeClassNotAllowed(DatabindContext.java:291)
at com.fasterxml.jackson.databind.DatabindContext.resolveAndValidateSubType(DatabindContext.java:248)
at com.fasterxml.jackson.databind.jsontype.impl.ClassNameIdResolver._typeFromId(ClassNameIdResolver.java:72)
at com.fasterxml.jackson.databind.jsontype.impl.ClassNameIdResolver.typeFromId(ClassNameIdResolver.java:66)
at com.fasterxml.jackson.databind.jsontype.impl.TypeDeserializerBase._findDeserializer(TypeDeserializerBase.java:156)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:97)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromAny(AsArrayTypeDeserializer.java:71)
at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserializeWithType(UntypedObjectDeserializer.java:712)
at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:68)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4202)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3309)
at org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer.deserialize(Jackson2JsonRedisSerializer.java:73)
... 79 more
I have read the Spring document in https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#redis:serializer. But still have no idea how to fix the problem.
Any advice? thanks!
As deprecated method is still available, we can stole the implementation from there :)
This is how it looks in my code now:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance , DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
The main point here is LaissezFaireSubTypeValidator.instance, cause it creates the necessary PolymorphicTypeValidator.

when using JpaRepository o.h.i.SessionFactoryImpl : HHH000031: Closing

I have a problem when starting spring-boot appication with Using JpaRepository.
more details, deleting findReviewByUser_num inside ReviewRepository works well.
My code did not have a Controller, Service, and I was creating Repository and just testing.
Review.Java
package org.soma.tripper.review.entity;
import lombok.*;
import javax.persistence.*;
import java.util.List;
#Getter
#Entity
#NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Review {
#Id
#GeneratedValue(strategy= GenerationType.IDENTITY)
private int review_num;
#Column(name = "user_num")
private int user_num;
#Column(name = "schedule_num")
private String schedule_num;
#Column(name = "content",length = 1000)
private String content;
#Column(name="rating")
private double rating;
#Column(name="ml_rating")
private double ml_rating;
#Builder Review(int user_num, int schedule_num, String content, double rating){
this.user_num=user_num;
this.user_num = schedule_num;
this.content=content;
this.rating = rating;
}
}
ReviewRepository.java
package org.soma.tripper.review.repository;
import org.soma.tripper.review.entity.Review;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ReviewRepository extends JpaRepository<Review,Integer> {
Review findReviewByUser_num(int user_num);
}
build.gradle
buildscript {
ext {
springBootVersion = '2.0.3.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
jar {
manifest {
attributes 'Main-Class': 'org.soma.tripper.TripperApplication'
}
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
}
group = 'org.soma'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
//swagger
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.5.0'
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.5.0'
//jpa
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compileOnly('org.projectlombok:lombok')
//logback
compile('ch.qos.logback:logback-classic:1.2.3')
compile('ch.qos.logback:logback-core:1.2.3')
compile 'log4j:log4j:1.2.17'
compile 'org.slf4j:slf4j-api:1.7.5'
compile 'org.slf4j:slf4j-log4j12:1.7.5'
// S3
// https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk
compile group: 'com.amazonaws', name: 'aws-java-sdk', version: '1.11.371'
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.6'
compile('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
console

spring form validation using custom annotation

I am using custom annotation for form validation in my spring4.2.3 and hibernate5.1.0 based project.
1) My Annotation Interface looks like this
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
import com.rapidtech.rapidtechorganic.custom.logic.UnitConstraintValidator;
#Documented
#Constraint(validatedBy = UnitConstraintValidator.class)
#Target( {ElementType.FIELD})
#Retention(RetentionPolicy.RUNTIME)
public #interface IsUnit {
String message() default "Please Enter a valid UNIT e.g. kg,litre,ton";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
2) My logic class UnitConstraintValidator looks like this
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import com.rapidtech.rapidtechorganic.custom.annotation.IsUnit;
public class UnitConstraintValidator implements ConstraintValidator<IsUnit, String> {
#Override
public void initialize(IsUnit unit) {
// TODO Auto-generated method stub
}
#Override
public boolean isValid(String unit, ConstraintValidatorContext cxt) {
if(unit.equals(null) || unit == null){
return false;
}
if(unit.matches("kg|KG|Kg|Litre|litre|lt|ton|Ton")){
return true;
}
return false;
}
}
3) My Model class where I am using custom annotation
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.validation.constraints.Size;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.rapidtech.rapidtechorganic.custom.annotation.IsUnit;
#JsonIgnoreProperties(ignoreUnknown = true)
#Entity
public class Lot implements java.io.Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private long lotId;
#Size(min=5,max=30)
private String lotNumber;
#IsUnit
private String lotName;
#OneToOne(cascade = {CascadeType.ALL})
private ProductAvailable productAvailable;
public long getLotId() {
return lotId;
}
public void setLotId(long lotId) {
this.lotId = lotId;
}
public String getLotNumber() {
return lotNumber;
}
public void setLotNumber(String lotNumber) {
this.lotNumber = lotNumber;
}
public String getLotName() {
return lotName;
}
public void setLotName(String lotName) {
this.lotName = lotName;
}
public ProductAvailable getProductAvailable() {
return productAvailable;
}
public void setProductAvailable(ProductAvailable productAvailable) {
this.productAvailable = productAvailable;
}
}
4) My ProductAvailable model class where I am using custom annotation
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.validation.constraints.Size;
import com.rapidtech.rapidtechorganic.custom.annotation.IsUnit;
#Entity
public class ProductAvailable implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private long productAvailableId;
#Size(min=2,max=30,message="ahha ahha ahha")
private String productAvailableName;
#IsUnit
private String productAvailableUnit;
private double productAvailableUnitTransportationCharge;
private double productAvailableUnitLabourCharge;
private double productAvailableUnitOtherCharge;
private long productAvailableQuantity;
private double productAvailableUnitPrice;
#OneToOne
private Lot lot;
public long getProductAvailableId() {
return productAvailableId;
}
public void setProductAvailableId(long productAvailableId) {
this.productAvailableId = productAvailableId;
}
public String getProductAvailableName() {
return productAvailableName;
}
public void setProductAvailableName(String productAvailableName) {
this.productAvailableName = productAvailableName;
}
public long getProductAvailableQuantity() {
return productAvailableQuantity;
}
public void setProductAvailableQuantity(long productAvailableQuantity) {
this.productAvailableQuantity = productAvailableQuantity;
}
public double getProductAvailableUnitPrice() {
return productAvailableUnitPrice;
}
public void setProductAvailableUnitPrice(double productAvailableUnitPrice) {
this.productAvailableUnitPrice = productAvailableUnitPrice;
}
public String getProductAvailableUnit() {
return productAvailableUnit;
}
public void setProductAvailableUnit(String productAvailableUnit) {
this.productAvailableUnit = productAvailableUnit;
}
public double getProductAvailableUnitTransportationCharge() {
return productAvailableUnitTransportationCharge;
}
public void setProductAvailableUnitTransportationCharge(double productAvailableUnitTransportationCharge) {
this.productAvailableUnitTransportationCharge = productAvailableUnitTransportationCharge;
}
public double getProductAvailableUnitLabourCharge() {
return productAvailableUnitLabourCharge;
}
public void setProductAvailableUnitLabourCharge(double productAvailableUnitLabourCharge) {
this.productAvailableUnitLabourCharge = productAvailableUnitLabourCharge;
}
public double getProductAvailableUnitOtherCharge() {
return productAvailableUnitOtherCharge;
}
public void setProductAvailableUnitOtherCharge(double productAvailableUnitOtherCharge) {
this.productAvailableUnitOtherCharge = productAvailableUnitOtherCharge;
}
public Lot getLot() {
return lot;
}
public void setLot(Lot lot) {
this.lot = lot;
}
}
I am using hibernate-validator4.1.0.Final and JSR 303 for validation.
As you can see that I have two model classes Lot and ProductAvailable with mapping OneToMany that means one Lot can have Many ProductAvailable.
My Problem is: My custom annotaion IsUnit working fine with class Lot but throwing exception in case of class ProductAvailable.
Exception is:
javax.validation.ConstraintViolationException: Validation failed for classes [com.rapidtech.rapidtechorganic.model.ProductAvailable] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='Please Enter a valid UNIT e.g. kg,litre,ton', propertyPath=productAvailableUnit, rootBeanClass=class com.rapidtech.rapidtechorganic.model.ProductAvailable, messageTemplate='Please Enter a valid UNIT e.g. kg,litre,ton'}
]
org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:138)
org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:78)
org.hibernate.action.internal.EntityInsertAction.preInsert(EntityInsertAction.java:205)
org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:82)
org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:560)
org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:434)
org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1295)
org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:468)
org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3135)
org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2352)
org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485)
org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:147)
org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231)
org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65)
com.rapidtech.rapidtechorganic.dao.AbstractDao.save(AbstractDao.java:30)
com.rapidtech.rapidtechorganic.dao.lot.LotDaoImpl.save(LotDaoImpl.java:22)
com.rapidtech.rapidtechorganic.service.LotServiceImpl.save(LotServiceImpl.java:25)
com.rapidtech.rapidtechorganic.controller.LotController.saveLotController(LotController.java:59)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
My Controller class looks like this:
#RequestMapping(value = "/saveLot", method = RequestMethod.POST)
public ModelAndView saveLotController(#Valid #ModelAttribute(value="lot") Lot lot, BindingResult result) {
ModelAndView model;
if(result.hasErrors()){
model = new ModelAndView("saveLotForm");
}
else {
Services lotService = (LotServiceImpl) appContext.getBean("lotServiceImpl");
lot.getProductAvailable().setLot(lot);
lotService.save(lot);
model = new ModelAndView("Success");
model.addObject("successMessage","Lot "+lot.getLotName()+" Saved Successfully!");
}
return model;
}
Any help would be appreaciated and Thanks in Advance.
Afte doing some research I have found the solution. All I need to put #Valid annotation over my productAvailable member variable of class Lot
Then it will check the validation for my ProductAvailable class.
How to Validate different model class into one form using Spring MVC JSR-303 Validator

How to specify spring-data-rest version in spring-boot project?

I have a spring-boot project and I'm using spring data rest with it. My build.gradle file looks like this. As you can see I did everything by the manual (well, apparently not everything).
What I want is to have /profile link and ability to get json-schema for all endpoints that I'm publishing. Instead I have /apls link. So I've checked spring-data-rest manual for <2.4 version and it doesn't mention neither /profile link nor json-schema. So I've figured that I'm not using the latest version of spring-data-rest.
I've added spring boot gradle plugin and I'm not specifying version for spring-boot-data-rest dependency. I've also tried to add org.springframework.data:spring-data-rest-webmvc:2.4.0.RELEASE dependency.
But that apparently doesn't work, because I still have /alps link instead of /profile link.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'settings'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
dependencies {
compile group: 'org.zeromq', name: 'jeromq', version: '0.3.5'
compile group: 'org.json', name: 'json', version: '20141113'
compile group: 'org.apache.commons', name: 'commons-io', version: '1.3.2'
compile group: 'org.skyscreamer', name: 'jsonassert', version: '1.2.3'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-rest'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-hateoas'
compile group: 'postgresql', name: 'postgresql', version:'9.1-901-1.jdbc4'
compile("org.springframework.data:spring-data-rest-webmvc:2.4.0.RELEASE")
runtime group: 'com.h2database', name: 'h2', version:'1.4.187'
testCompile(group: 'org.springframework.boot', name: 'spring-boot-starter-test') {
exclude(module: 'commons-logging')
}
}
EDIT1:
I've found that if I'm not including dependency
compile("org.springframework.data:spring-data-rest-webmvc:2.4.0.RELEASE")
Than gradle using spring-data-rest 2.2.3 or something like that.
But when I'm including that dependency it uses 2.4.0 like it should be, but then my test is failing for some reason.
My test looks like that
package demo;
import demo.settings.DemoApplication;
import demo.settings.processing.Channel;
import demo.settings.processing.ChannelMode;
import demo.settings.processing.ChannelsController;
import demo.settings.processing.ChannelRepository;
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.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.util.List;
import static org.junit.Assert.*;
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = DemoApplication.class)
public class DemoApplicationTests {
final String BASE_URL = "http://localhost:8080/";
private MockMvc mockMvc;
#Autowired
private ChannelsController controller;
#Autowired
private ChannelRepository repository;
#Before
public void setup() {
this.mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}
#Test
public void setChannels() {
Channel exampleChannel = new Channel(ChannelMode.AUTO, 1, 1, 1, false, 0);
controller.setChannels(0, 10, exampleChannel);
List<Channel> allChannels = repository.findAllByOrderByBinIndexAsc();
for (int i = 0; i <= 10; i++) {
Channel ch = allChannels.get(i);
assertEquals(ch.getBinIndex(), i);
assertEquals(ch.getC1(), exampleChannel.getC1(), 0);
assertEquals(ch.getC2(), exampleChannel.getC2(), 0);
assertEquals(ch.getManualCoefficient(), exampleChannel.getManualCoefficient(), 0);
assertEquals(ch.getMode().toString(), exampleChannel.getMode().toString());
assertEquals(ch.isExcluded(), exampleChannel.isExcluded());
}
exampleChannel.setC1(100);
controller.setChannels(0, 11, exampleChannel);
allChannels = repository.findAllByOrderByBinIndexAsc();
for (int i = 0; i <= 11; i++) {
Channel ch = allChannels.get(i);
assertEquals(ch.getBinIndex(), i);
assertEquals(ch.getC1(), exampleChannel.getC1(), 0);
assertEquals(ch.getC2(), exampleChannel.getC2(), 0);
assertEquals(ch.getManualCoefficient(), exampleChannel.getManualCoefficient(), 0);
assertEquals(ch.getMode().toString(), exampleChannel.getMode().toString());
assertEquals(ch.isExcluded(), exampleChannel.isExcluded());
}
}
}
Here is my repository
#RepositoryRestResource(path="dts_stm32_settings")
interface DtsStm32SettingsRepository extends PagingAndSortingRepository<DtsStm32Settings, Long> {
}
Here is my Model
package demo.settings.data_collection.stm;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
/**
* Created by michael on 11/09/15.
*/
#Entity
public class DtsStm32Settings extends Stm32Settings {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
#NotNull #Min(value=0) #Max(value=65535)
private int firstChannelDac;
#NotNull #Min(value=0) #Max(value=65535)
private int secondChannelDac;
#NotNull #Min(value=0) #Max(value=65535)
private int dil;
#NotNull
private CommutatorChannel commutatorChannel;
#NotNull #Min(value=0) #Max(value=65535)
private int firstChannelPwm;
#NotNull #Min(value=0) #Max(value=65535)
private int zeroChannelPwm;
public DtsStm32Settings() {
}
public DtsStm32Settings(
int firstChannelShift,
int secondChannelShift,
int firstChannelGain,
int secondChannelGain,
int firstChannelSlope,
int secondChannelSlope,
boolean led,
boolean firstChannelDurationBit,
boolean secondChannelDurationBit,
int firstChannelDac,
int secondChannelDac,
int dil,
CommutatorChannel commutatorChannel,
int firstChannelPwm,
int zeroChannelPwm,
boolean pulsedPumpMode,
int durationOn,
int durationOff
) {
super(firstChannelShift, secondChannelShift, firstChannelGain, secondChannelGain, firstChannelSlope, secondChannelSlope, led, firstChannelDurationBit, secondChannelDurationBit);
this.firstChannelDac = firstChannelDac;
this.secondChannelDac = secondChannelDac;
this.dil = dil;
this.commutatorChannel = commutatorChannel;
this.firstChannelPwm = firstChannelPwm;
this.zeroChannelPwm = zeroChannelPwm;
this.pulsedPumpMode = pulsedPumpMode;
this.durationOn = durationOn;
this.durationOff = durationOff;
}
#NotNull
private boolean pulsedPumpMode;
#NotNull #Min(value=1) #Max(value=65535)
private int durationOn;
#NotNull #Min(value=0) #Max(value=65535)
private int durationOff;
public int getFirstChannelPwm() {
return firstChannelPwm;
}
public void setFirstChannelPwm(int firstChannelPwm) {
this.firstChannelPwm = firstChannelPwm;
}
public int getZeroChannelPwm() {
return zeroChannelPwm;
}
public void setZeroChannelPwm(int zeroChannelPwm) {
this.zeroChannelPwm = zeroChannelPwm;
}
public int getFirstChannelDac() {
return firstChannelDac;
}
public void setFirstChannelDac(int firstChannelDac) {
this.firstChannelDac = firstChannelDac;
}
public int getSecondChannelDac() {
return secondChannelDac;
}
public void setSecondChannelDac(int secondChannelDac) {
this.secondChannelDac = secondChannelDac;
}
public int getDil() {
return dil;
}
public void setDil(int dil) {
this.dil = dil;
}
public CommutatorChannel getCommutatorChannel() {
return commutatorChannel;
}
public void setCommutatorChannel(CommutatorChannel commutatorChannel) {
this.commutatorChannel = commutatorChannel;
}
public boolean isPulsedPumpMode() {
return pulsedPumpMode;
}
public void setPulsedPumpMode(boolean pulsedPumpMode) {
this.pulsedPumpMode = pulsedPumpMode;
}
public int getDurationOn() {
return durationOn;
}
public void setDurationOn(int durationOn) {
this.durationOn = durationOn;
}
public int getDurationOff() {
return durationOff;
}
public void setDurationOff(int durationOff) {
this.durationOff = durationOff;
}
}
package demo.settings.data_collection.stm;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
/**
* Created by michael on 11/09/15.
*/
#JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, include=JsonTypeInfo.As.PROPERTY, property = "type")
#JsonSubTypes({#JsonSubTypes.Type(DtsStm32Settings.class)})
abstract class Stm32Settings {
#NotNull
#Min(value=0) #Max(value=65535)
protected int firstChannelShift;
#NotNull
#Min(value=0) #Max(value=65535)
protected int secondChannelShift;
#NotNull
#Min(value=0) #Max(value=65535)
protected int firstChannelGain;
#NotNull
#Min(value=0) #Max(value=65535)
protected int secondChannelGain;
#NotNull
#Min(value=0) #Max(value=65535)
protected int firstChannelSlope;
#NotNull
#Min(value=0) #Max(value=65535)
protected int secondChannelSlope;
#NotNull
protected boolean led;
#NotNull
protected boolean firstChannelDurationBit;
#NotNull
protected boolean secondChannelDurationBit;
protected Stm32Settings() {
}
public int getFirstChannelShift() {
return firstChannelShift;
}
public void setFirstChannelShift(int firstChannelShift) {
this.firstChannelShift = firstChannelShift;
}
public int getSecondChannelShift() {
return secondChannelShift;
}
public void setSecondChannelShift(int secondChannelShift) {
this.secondChannelShift = secondChannelShift;
}
public int getFirstChannelGain() {
return firstChannelGain;
}
public void setFirstChannelGain(int firstChannelGain) {
this.firstChannelGain = firstChannelGain;
}
public int getSecondChannelGain() {
return secondChannelGain;
}
public void setSecondChannelGain(int secondChannelGain) {
this.secondChannelGain = secondChannelGain;
}
public int getFirstChannelSlope() {
return firstChannelSlope;
}
public void setFirstChannelSlope(int firstChannelSlope) {
this.firstChannelSlope = firstChannelSlope;
}
public int getSecondChannelSlope() {
return secondChannelSlope;
}
public void setSecondChannelSlope(int secondChannelSlope) {
this.secondChannelSlope = secondChannelSlope;
}
public boolean isLed() {
return led;
}
public void setLed(boolean led) {
this.led = led;
}
public boolean isFirstChannelDurationBit() {
return firstChannelDurationBit;
}
public void setFirstChannelDurationBit(boolean firstChannelDurationBit) {
this.firstChannelDurationBit = firstChannelDurationBit;
}
public boolean isSecondChannelDurationBit() {
return secondChannelDurationBit;
}
public void setSecondChannelDurationBit(boolean secondChannelDurationBit) {
this.secondChannelDurationBit = secondChannelDurationBit;
}
public Stm32Settings(
int firstChannelShift,
int secondChannelShift,
int firstChannelGain,
int secondChannelGain,
int firstChannelSlope,
int secondChannelSlope,
boolean led,
boolean firstChannelDurationBit,
boolean secondChannelDurationBit
) {
setFirstChannelShift(firstChannelShift);
setSecondChannelShift(secondChannelShift);
setFirstChannelGain(firstChannelGain);
setSecondChannelGain(secondChannelGain);
setFirstChannelSlope(firstChannelSlope);
setSecondChannelSlope(secondChannelSlope);
setLed(led);
setFirstChannelDurationBit(firstChannelDurationBit);
setSecondChannelDurationBit(secondChannelDurationBit);
}
}
And here is error which tells me about nothing
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stm32Controller': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private demo.settings.data_collection.stm.DtsStm32SettingsRepository demo.settings.data_collection.stm.Stm32Controller.repository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dtsStm32SettingsRepository': Invocation of init method failed; nested exception is java.lang.AbstractMethodError: org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg/springframework/data/repository/core/RepositoryInformation;)Ljava/lang/Object;
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:687)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
at org.springframework.boot.test.SpringApplicationContextLoader.loadContext(SpringApplicationContextLoader.java:104)
at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:68)
at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:86)
... 45 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private demo.settings.data_collection.stm.DtsStm32SettingsRepository demo.settings.data_collection.stm.Stm32Controller.repository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dtsStm32SettingsRepository': Invocation of init method failed; nested exception is java.lang.AbstractMethodError: org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg/springframework/data/repository/core/RepositoryInformation;)Ljava/lang/Object;
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 60 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dtsStm32SettingsRepository': Invocation of init method failed; nested exception is java.lang.AbstractMethodError: org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg/springframework/data/repository/core/RepositoryInformation;)Ljava/lang/Object;
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 62 more
Caused by: java.lang.AbstractMethodError: org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg/springframework/data/repository/core/RepositoryInformation;)Ljava/lang/Object;
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:185)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
... 72 more
Try specifying Spring Data release train instead:
dependencyManagement {
imports {
...
mavenBom "org.springframework.data:spring-data-releasetrain:Gosling-RELEASE"
}
}
Then just compile the spring-data starters you need without specifying the version!

Resources