I am trying to switch from xml config to java config and getting the following exception when starting the application.
Caused by: java.lang.ClassCastException: $Proxy188 cannot be cast to org.springframework.format.support.FormattingConversionService
This exception is thrown on the first line of the following method:
#Override
public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
RequestMappingHandlerAdapter adapter = super.requestMappingHandlerAdapter();
adapter.setIgnoreDefaultModelOnRedirect(true);
return adapter;
}
Any idea why this is failing?
Related
I have the following bean definition.
#Bean
public DataSource hikariDataSource() {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl(JDBC_URL);
hikariConfig.setUsername(USERNAME);
hikariConfig.setPassword(PASSWORD);
return new HikariDataSource(hikariConfig);
}
When I get application context.
ApplicationContext ctx = new AnnotationConfigApplicationContext("org.example");
An exception is being thrown
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hikariDataSource' defined in org.example.config.Configuration: Failed to instantiate [javax.sql.DataSource]: Factory method 'hikariDataSource' threw exception with message: null
Is there any idea what can be the reason?
Maybe some of the answers from this thread will help.
https://stackoverflow.com/a/33213802/3816621
Also please check if you get #Configuration annotation on class level.
We are getting exception
Error creating bean with name 'costsEntityManagerFactory' defined in class path resource...Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: consolidatedCosts] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.search.util.common.SearchException: HSEARCH000573: Invalid configuration passed to Hibernate Search: some properties in the given configuration are obsolete.Configuration properties changed between Hibernate Search 5 and Hibernate Search 6 Check out the reference documentation and upgrade your configuration.
Here is the code snippet
#Bean
public LocalContainerEntityManagerFactoryBean costsEntityManagerFactory(
final EntityManagerFactoryBuilder builder,
final #Qualifier("awsCostsDataSource") DataSource costsDataSource) {
final Map<String, String> properties = new HashMap<>();
properties.put(
"hibernate.dialect",
"com.x.y.consolidatedcosts.client.xxxxxPostgresDialect");
properties.put(
"hibernate.physical_naming_strategy", CostsTableNamingStrategy.class.getName());
return builder.dataSource(costsDataSource)
.packages("com.xxx.xxx.consolidatedcosts")
.persistenceUnit("consolidatedCosts")
.properties(properties)
.build();
}
I am using the spring cloud contrat to test a kafkaListsener of my service. I use EmbeddedKafka together with the spring cloud contract.
The listener in my service is implemeneted by #KafkaListener from spring kafka.
The contract test I have is like below:
#EmbeddedKafka
#SpringBootTest(classes = {ServiceApplication.class},
properties = {"stubrunner.kafka.enabled=true",
"stubrunner.stream.enabled=false",
"spring.cloud.stream.function.autodetect=false"})
public class EventContractTest {
#Autowired
private StubTrigger stubTrigger;
#SpyBean
#Qualifier("KafkaEventListener")
private EventListener eventListener;
#BeforeEach
public void setup() throws ExecutionException, InterruptedException {
Mockito.doNothing().when(eventListener).onEventReceived(any(), any());
}
#Test
public void kafkaEventTest() throws ExecutionException, InterruptedException {
stubTrigger.trigger("kafka-event");
ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class);
ArgumentCaptor<Bytes> headerArgumentCaptor = ArgumentCaptor.forClass(Bytes.class);
verify(eventListener, timeout(5000).times(1)).
onEventReceived(eventArgumentCaptor.capture(), headerArgumentCaptor.capture());
....
}
#Test
public void kafkaEventTest2() throws ExecutionException, InterruptedException {
stubTrigger.trigger("kafka-event-2");
ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class);
ArgumentCaptor<Bytes> headerArgumentCaptor = ArgumentCaptor.forClass(Bytes.class);
verify(eventListener, timeout(5000).times(1)).
onEventReceived(eventArgumentCaptor.capture(), headerArgumentCaptor.capture());
....
}
}
when run the tests case, in most cases it will pass, but it will randomlly fail with the following exception:
givenEventFromContract_whenCancelSubscription_thenTriggerSubscriptionChangedKafkaEvent
Time elapsed: 0.002 s <<< ERROR! 14:12:43
java.lang.IllegalStateException: Failed to load ApplicationContext
14:12:43 Caused by:
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'embeddedKafka': Invocation of init method
failed; nested exception is kafka.common.KafkaException: 14:12:43
Caused by: kafka.common.KafkaException: 14:12:43 Caused by:
java.util.concurrent.TimeoutException 14:12:43 14:12:43 [ERROR]
givenEventFromContract_whenDeleteSubscription_thenTriggerSubscriptionDeletedKafkaEvent
Time elapsed: 25.891 s <<< ERROR! 14:12:43
java.lang.IllegalStateException: java.util.concurrent.TimeoutException
14:12:43 at
And below are my kafka relevant configuration:
# -- Kafka with PLAINTEXT authentication --
com.sap.kafka.enabled=false
spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}
spring.kafka.consumer.group-id=test
spring.kafka.consumer.properties.auto.offset.reset=earliest
spring.kafka.topics.default.replication-factor=1
spring.kafka.topics.default.partition-count=1
spring.kafka.topics.default.min-in-sync-replicas=1
Does someone know what was the issue? is this a bug of spring kafka?
We are getting the below mentioned error on websphere 8.5
2020-01-07 15:19:37 [] DEBUG InvocableHandlerMethod.java.getMethodArgumentValues:174:
Could not resolve parameter [1] in public org.springframework.http.ResponseEntity
com.mycorp.uap.controller.WorkFlowController.updateTask(int,com.mycorp.uap.rest.vo.TaskVO)
throws java.lang.Exception: HV000041: Call to TraversableResolver.isReachable() threw an exception.
Method is defined as
public ResponseEntity<TaskVO> updateTask(#PathVariable("id") int taskId, #Valid #RequestBody(required=true) TaskVO task) throws Exception{
WEB-INF/lib contains the below jars related to hibernate, validation and spring
hibernate-commons-annotations-5.1.0.Final.jar
hibernate-core-5.4.4.Final.jar
hibernate-ehcache-5.4.4.Final.jar
hibernate-jpa-2.1-api-1.0.2.jar
hibernate-validator-6.0.15.Final.jar
validation-api-2.0.1.Final.jar
spring-aop-5.1.9.RELEASE.jar
spring-beans-5.1.9.RELEASE.jar
spring-context-5.1.9.RELEASE.jar
spring-context-support-5.1.9.RELEASE.jar
spring-core-5.1.9.RELEASE.jar
spring-data-commons-2.1.9.RELEASE.jar
spring-data-jpa-2.1.9.RELEASE.jar
spring-expression-5.1.9.RELEASE.jar
springfox-core-2.1.2.jar
springfox-schema-2.1.2.jar
springfox-spi-2.1.2.jar
springfox-spring-web-2.1.2.jar
springfox-swagger2-2.1.2.jar
springfox-swagger-common-2.1.2.jar
springfox-swagger-ui-2.1.2.jar
spring-hateoas-0.17.0.RELEASE.jar
spring-jcl-5.1.9.RELEASE.jar
spring-jdbc-5.1.9.RELEASE.jar
spring-ldap-core-2.3.2.RELEASE.jar
spring-messaging-5.1.9.RELEASE.jar
spring-orm-5.1.9.RELEASE.jar
spring-plugin-core-1.2.0.RELEASE.jar
spring-plugin-metadata-1.2.0.RELEASE.jar
spring-security-acl-5.1.6.RELEASE.jar
spring-security-cas-client.jar
spring-security-config-5.1.6.RELEASE.jar
spring-security-core-5.1.6.RELEASE.jar
spring-security-ldap-5.1.6.RELEASE.jar
spring-security-oauth2-2.3.6.RELEASE.jar
spring-security-openid-5.1.6.RELEASE.jar
spring-security-taglibs-5.1.6.RELEASE.jar
spring-security-web-5.1.6.RELEASE.jar
spring-test-5.1.9.RELEASE.jar
spring-tx-5.1.9.RELEASE.jar
spring-web-5.1.9.RELEASE.jar
spring-webmvc-5.1.9.RELEASE.jar
spring-websocket-5.1.9.RELEASE.jar
ParentLast setting is present in WebSphere configuration for our application so that WebSphere should give preference to the jars present in the WEB-INF/lib of our application
There is a similar method where #Valid is not present which works properly.
I looked into similar question on the stack overflow however could not quite get the correct solution.
What should be the correct solution?
Should we remove any jars from our WEB-INF/lib?
To fix this problem, you need to add the HibernatePersistenceProviderResolver class to your project:
HibernatePersistenceProviderResolver.java
and register it in the Application class in the onStartup method
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
HibernatePersistenceProviderResolver.register();
...
}
Reference
I have implemented a simple file upload routine using Spring Boot ConfigurationProperties annotation so I can load the directory from my YAML configuration file:
Service:
#Service
public class FileSystemStorageService implements StorageService {
private final Logger log = LoggerFactory.getLogger(FileSystemStorageService.class);
private final Path pictureLocation;
#Autowired
public FileSystemStorageService(StorageProperties storageProperties) {
pictureLocation = Paths.get(storageProperties.getUpload());
}
And the StorageProperties:
#Component
#ConfigurationProperties("nutrilife.meals")
public class StorageProperties {
private String upload;
public String getUpload() {
return upload;
}
public void setUpload(String upload) {
this.upload= upload;
}
}
In the yaml file I have:
nutrilife:
meals:
upload: /$full_path_to_my_upload_dir
This works perfectly in normal Spring Boot runtime but the problem starts when I try to run my integration tests, it throws the error:
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fileSystemStorageService' defined in file [/home/mmaia/git/nutrilife/build/classes/main/com/getnutrilife/service/upload/FileSystemStorageService.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.getnutrilife.service.upload.FileSystemStorageService]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:279)
...
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.getnutrilife.service.upload.FileSystemStorageService]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154)
So basically during tests the YAML file configuration looks like it's not being properly picked up. I am new to Spring. How can I fix it?
try adding the #value annotation :
#Value("upload")
private String upload;
The above answer is valid for application.properties config.
it also works with yaml as well.
You may find your correct correct yaml config here :
24.6 Using YAML instead of Properties