ConverterNotFoundException: -Dserver.port=80-noverify No converter found ... String to Integer - spring-boot

I'm starting my Spring Boot application from Eclipse with this VM parameter
-Dserver.port=80
application.yml
server:
port: 8080
but I get the following exception "No converter found capable of converting from type [java.lang.String] to type [java.lang.Integer]".
Running with Spring Boot v1.5.4.RELEASE, Spring v4.3.9.RELEASE
...
2017-07-19 11:26:55.299 ERROR 5968 --- [ main] o.s.b.b.PropertiesConfigurationFactory : Properties configuration failed validation
2017-07-19 11:26:55.299 ERROR 5968 --- [ main] o.s.b.b.PropertiesConfigurationFactory : Field error in object 'server' on field 'port': rejected value [80-noverify]; codes [typeMismatch.server.port,typeMismatch.port,typeMismatch.java.lang.Integer,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [server.port,port]; arguments []; default message [port]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'port'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [java.lang.Integer]]
Field error in object 'server' on field 'port': rejected value [80-noverify]; codes [typeMismatch.server.port,typeMismatch.port,typeMismatch.java.lang.Integer,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [server.port,port]; arguments []; default message [port]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'port'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [java.lang.Integer]]
...
***************************
APPLICATION FAILED TO START
***************************
Description:
Binding to target org.springframework.boot.autoconfigure.web.ServerProperties#caaddaea failed:
Property: server.port
Value: 80-noverify
Reason: Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'port'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [java.lang.Integer]
Action:
Update your application's configuration
If I use the property server.port in my application.yml it works, no problem.
What am I doing wrong? Thanks for any help.

The issue is caused by the spring boot run configuration: In the run configuration, just disable "Fast startup" and the problem will go away. This option causes -noverify to be added as a VM argument, but, unfortunately, with no spaces.

Related

Feign Retry : No converter found capable of converting from type [java.lang.String] to type [java.lang.Class<feign.Retryer>]

I am getting the below error while using feign retryer. I am using
io.github.openfeign.feign-core -> v10.10.1
io.github.openfeign.feign-hystrix -> v10.10.1
io.github.openfeign.feign-form -> v3.8.0
Feign Retryer Error - No converter found capable of converting from type [java.lang.String] to type [java.lang.Class<feign.Retryer>]
I have the following configuration in my application yml.
feign:
client:
config:
default:
connectTimeout: 60000000
readTimeout: 60000000
client-service:
error-decoder: testservice.exception.FeignClientErrorDecoder
retryer: testservice.exception.FeignClientRetryer
Failed to bind properties under 'feign.client.config.client-service.retryer' to java.lang.Class<feign.Retryer>:
Property: feign.client.config.client-service.retryer
Value: testservice.exception.FeignClientRetryer
Origin: class path resource [application.yml]:56:18
Reason: No converter found capable of converting from type [java.lang.String] to type [java.lang.Class<feign.Retryer>]
Action:
Update your application's configuration
Best Regards,
Manuag
Pre-requisite for your code to run as is:
FeignClientRetryer must be implementing feign.Retryer
FeignClientRetryer must have #Component stereotype on top of the class.
#FeignClient should have value="client-service"
If it still does not run, then You can try this:
Remove #Component from FeignClientRetryer
Define a configuration class:
public class ClientServiceFeignConfig{
#Bean
public Retryer clientServiceRetryer(){
return new FeignClientRetryer();
}
}
And, on top of your client-service, add:
#FeignClient(value="client-service", configuration = ClientServiceFeignConfig.class)

org.springframework.beans.ConversionNotSupportedException:Failed to convert value of type 'java.lang.String' to required type 'java.util.Optional'

For the first time i am using Optional in Rest Web Service to make a Path Parameter as optional.
#RequestMapping(value = {"/hosts","/hosts/{name}"}, method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<Object> getHosts(#PathVariable Optional<String> name) {
if(name.isPresent()) {
return service.getHosts(name.get());
} else {
return service.getHosts(null);
}
}
When i use the API without the parameter, it works fine.
But when i give the parameter, it throws the following exception:
org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Optional'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Optional]: no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:74)
at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:47)
at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:603)
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:104)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
I have spent quite a time debugging this but could't get past this error. Any help is appreciated.
Thanks,
Just add "mvc:annotation-driven/> to your xml file to make it work.

java8 spring 4.2.5 can't convert to java.util.Optional

I have a problem with spring 4.2.5 and java8.
Trying to write simple rest:
#RequestMapping(value = "raw2", method = RequestMethod.GET)
ResponseEntity<String> getTest(#RequestParam Optional<String> p) {
return new ResponseEntity<>(HttpStatus.OK);
}
But keep getting errors:
2016-04-05 20:51:41,436 DEBUG [o.s.web.servlet.DispatcherServlet] DispatcherServlet with name 'dispatcher' processing GET request for [/internal/raw2]
2016-04-05 20:51:41,438 DEBUG [o.s.w.s.m.a.DefaultAnnotationHandlerMapping] Mapping [/internal/raw2] to HandlerExecutionChain with handler [com.test.con] and 1 interceptor
2016-04-05 20:51:41,440 DEBUG [o.s.web.servlet.DispatcherServlet] Last-Modified value for [/internal/raw2] is: -1
2016-04-05 20:51:41,453 DEBUG [org.springframework.beans.BeanUtils] No property editor [java.util.OptionalEditor] found for type java.util.Optional according to 'Editor' suffix convention
2016-04-05 20:51:41,453 DEBUG [o.s.w.s.m.a.AnnotationMethodHandlerExceptionResolver] Resolving exception from handler [com.test.con]: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type [java.lang.String] to required type [java.util.Optional]; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Optional]: no matching editors or conversion strategy found
2016-04-05 20:51:41,454 DEBUG [o.s.w.s.m.a.ResponseStatusExceptionResolver] Resolving exception from handler [com.test.con]: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type [java.lang.String] to required type [java.util.Optional]; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Optional]: no matching editors or conversion strategy found
2016-04-05 20:51:41,454 DEBUG [o.s.w.s.m.s.DefaultHandlerExceptionResolver] Resolving exception from handler [com.test.con]: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type [java.lang.String] to required type [java.util.Optional]; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Optional]: no matching editors or conversion strategy found
2016-04-05 20:51:41,454 WARN [o.s.w.s.m.s.DefaultHandlerExceptionResolver] Failed to convert request element: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type [java.lang.String] to required type [java.util.Optional]; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Optional]: no matching editors or conversion strategy found
Ensure you have added the <mvc:annotation-driven> tag in XML. This will make sure that convertors are available

Spring Batch - "job" scoped beans can not be injected into "job" or "step" scoped beans

I am using spring batch version 3.0.2.RELEASE and spring framework version 3.2.12.RELEASE. And I am trying to inject a job scoped bean to another job scoped bean.
My configuration looks like this
<bean id="beanA" class="com.trial.BeanA" scope="job" >
<property name="beanB" ref="beanB" />
</bean>
<bean id="beanB" class="com.trial.BeanB" scope="job"/>
It throws an exception with details:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.beanA' defined in class path resource [trial-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.sun.proxy.$Proxy1 implementing com.trial.BeanB,org.springframework.beans.factory.InitializingBean,org.springframework.aop.scope.ScopedObject,java.io.Serializable,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'com.trial.BeanB' for property 'beanB'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy1 implementing com.trial.BeanB,org.springframework.beans.factory.InitializingBean,org.springframework.aop.scope.ScopedObject,java.io.Serializable,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.trial.BeanB] for property 'beanB': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:331)
at org.springframework.batch.core.scope.JobScope.get(JobScope.java:103)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:327)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:34)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:184)
at com.sun.proxy.$Proxy13.getValue(Unknown Source)
at org.springframework.batch.core.listener.CompositeJobExecutionListener.beforeJob(CompositeJobExecutionListener.java:73)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:301)
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49)
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.sun.proxy.$Proxy1 implementing com.trial.BeanB,org.springframework.beans.factory.InitializingBean,org.springframework.aop.scope.ScopedObject,java.io.Serializable,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'com.trial.BeanB' for property 'beanB'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy1 implementing com.trial.BeanB,org.springframework.beans.factory.InitializingBean,org.springframework.aop.scope.ScopedObject,java.io.Serializable,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.trial.BeanB] for property 'beanB': no matching editors or conversion strategy found
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:464)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:495)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:489)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1465)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1424)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1160)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
... 18 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy1 implementing com.trial.BeanB,org.springframework.beans.factory.InitializingBean,org.springframework.aop.scope.ScopedObject,java.io.Serializable,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.trial.BeanB] for property 'beanB': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:267)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:449)
... 24 more
Even I try to inject a "job" scoped bean to a "step" scoped bean, it fails and throws a similar exception.
How can I solve the issue?
You are using java proxying which requires interfaces, yet your BeanB doesn't implement an interface that BeanA is coded against. You need to either switch to dynamic subclassing or have BeanB implement an interface and have BeanA expect that interface.

Why can't #Value inject a JNDI value from the Environment?

I'm having trouble injecting JNDI values from my Tomcat into spring java config using the #Value annotation whereas I have no trouble retreiving the values via the Environment class. We are using Java 1.7.0_17, Spring 4.0.3, and Tomcat 7.0.52 .
I have in my context.xml the following variables defined:
<Environment name="USER_NAME" value="initech" type="java.lang.String" />
<Environment name="USER_PASSWORD" value="1n3+3ch!" type="java.lang.String" />
In my Java configuration file I have the following code working:
#Bean
public Foo foo( Environment env ){
return new Foo( env.getProperty("USER_NAME"), env.getProperty("USER_PASSWORD") );
}
When I look into the server log I see:
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.c.e.PropertySourcesPropertyResolver -> Searching for key 'USER_NAME' in [servletConfigInitParams]
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.c.e.PropertySourcesPropertyResolver -> Searching for key 'USER_NAME' in [servletContextInitParams]
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.c.e.PropertySourcesPropertyResolver -> Searching for key 'USER_NAME' in [jndiProperties]
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.springframework.jndi.JndiTemplate -> Looking up JNDI object with name [java:comp/env/USER_NAME]
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.jndi.JndiLocatorDelegate -> Located object with JNDI name [java:comp/env/USER_NAME]
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.jndi.JndiPropertySource -> JNDI lookup for name [USER_NAME] returned: [initech]
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.c.e.PropertySourcesPropertyResolver -> Found key 'USER_NAME' in [jndiProperties] with type [String] and value 'initech'
However, I would like to use #Value annotation to make the code cleaner if at all possible; something along the lines of
#Bean
public Foo foo(
#Value( "#{USER_NAME}" ) String userName,
#Value( "#{USER_PASSWORD}" ) String userPassword
){
return new Foo( userName, userPassword );
}
but this code results in the error:
nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'USER_NAME' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'USER_NAME' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'oauthTokenRequestClient' defined in class path resource [com/initech/set/gw/api/config/GatewayRepositoryConfig.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.net.URI]: : Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'USER_NAME' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'USER_NAME' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?
org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:747)
org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:462)
I have tried "#{systemEnvironment.USER_NAME}, "#{systemProperties.USER_NAME}, "#{jndiProperties.USER_NAME}, and "#{java:comp/env/USER_NAME} as well: yet, none of them work.
PS- I've tried to simplify the question so if one of these should have worked let me know because I may have made an error in my posting.
I solved it using the selected answer from the question Java Spring: How to use #Value annotation to inject an Environment property?.
So my code looks like:
#Bean
public Foo foo(
#Value( "#{environment.USER_NAME}" ) String userName,
#Value( "#{environment.USER_PASSWORD}" ) String userPassword
){
return new Foo( userName, userPassword );
}
I'm assuming from the content of the most upvoted answer (ie "assuming that you have a PropertySourcesPlaceHolderConfigurer registered") to that quesiton that this means we are lacking a configured PropertySourcesPlaceHolderConfigurer.
If you are using Spring 4 you should change
#Value( "#{USER_PASSWORD}" ) String userPassword
To
#Value( "${USER_PASSWORD}" ) String userPassword
Try that and see what happens.
For Spring 3.1+ (tested on 4.2.5.RELEASE), you can use #Value like this:
#Value("${property.name}")
private String jndiProperty;
But you also need to define a PropertySourcesPlaceholderConfigurer bean in a static method and configure a JndiPropertySource (I have this in an #Configuration class):
#Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(ConfigurableEnvironment env) {
PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer();
JndiPropertySource jndiPropertySource = new JndiPropertySource("java:comp");
env.getPropertySources().addFirst(jndiPropertySource);
return placeholderConfigurer;
}
Note the use of addFirst() which ensures property values from JNDI take precedence in case of a clash with other property sources, which may or may not be desired, in which case one can use other methods like addLast() etc.
This is the correct syntex
#Value("${PASSWORD}")
Have a look at this example:
http://www.programsji.com/spring/readpropertyfile

Resources