• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Failed to convert value of type 'java.lang.String' to required type

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an HTML page with a text field and a button.

When I fill the field and click enter I get the following error.

Failed to convert value of type  'java.lang.String' to required type '...model.Test'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Integer] for value 'typedvaluehere'

The code is below.

One interesting thing I noted is that when I remove extends CrudRepository<Test, Integer> from the repository, the error stops happening. But I can't do that, because I need it. But shows it seems to be a conflict with CrudRepository.

Another interesting thing I noted is that if I change "private String test" in the model to something else such as "private String name" and update getters/setters, it will work fine. Even with the CrudRepository. It doesn't work when there's a class attribute that has the same name of the model class name (ex Class Test with attribute test).

The complete project is available for download at: https://drive.google.com/file/d/1qfn73uGH6eUmJf8vtWZvbSn2IQiI0MzB/view?usp=sharing

AddTest.html



Controller




Model




Repository

 
Saloon Keeper
Posts: 12488
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really don't see how the CrudRepository could have anything to do with it, since you are not using it in your controller at all.
 
Jf Okeeffe
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, in the real project I do use it.
But in this test I removed it and the error still happens.

In fact, I still have these two lines (it's in the downloadable project I provided):

import projetos.repository.PessoaRepository;

@Autowired
private PessoaRepository pessoaRepository;

So, given your comment, I decided to comment those two out as well to see if the error would stop happening. No difference. The error continues there.

But if I go to the repository class and remove "extends CrudRepository<Pessoa, Integer>" the error stops happening.

So seems that even if the repository is not used, just for the fact that extending CrudRepository is there, something is being executed in the Spring backstage that is causing this error.
 
Stephan van Hulst
Saloon Keeper
Posts: 12488
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us the entire stack trace?
 
Jf Okeeffe
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what shows in the browser. In the Spring Tool Suit console no error is displayed.

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Nov 21 16:08:42 BRT 2020
There was an unexpected error (type=Bad Request, status=400).
Failed to convert value of type 'java.lang.String' to required type 'projetos.model.Pessoa'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Integer] for value 'wer'; nested exception is java.lang.NumberFormatException: For input string: "wer"
org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'projetos.model.Pessoa'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Integer] for value 'wer'; nested exception is java.lang.NumberFormatException: For input string: "wer"
at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:79)
at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:53)
at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:693)
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttributeFromRequestValue(ServletModelAttributeMethodProcessor.java:142)
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:78)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:139)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Integer] for value 'wer'; nested exception is java.lang.NumberFormatException: For input string: "wer"
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174)
at org.springframework.data.repository.support.DomainClassConverter$ToEntityConverter.convert(DomainClassConverter.java:177)
at org.springframework.data.repository.support.DomainClassConverter.lambda$convert$0(DomainClassConverter.java:85)
at java.base/java.util.Optional.map(Optional.java:258)
at org.springframework.data.repository.support.DomainClassConverter.convert(DomainClassConverter.java:85)
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:129)
at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:73)
... 52 more
Caused by: java.lang.NumberFormatException: For input string: "wer"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.valueOf(Integer.java:983)
at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:211)
at org.springframework.core.convert.support.StringToNumberConverterFactory$StringToNumber.convert(StringToNumberConverterFactory.java:64)
at org.springframework.core.convert.support.StringToNumberConverterFactory$StringToNumber.convert(StringToNumberConverterFactory.java:50)
at org.springframework.core.convert.support.GenericConversionService$ConverterFactoryAdapter.convert(GenericConversionService.java:436)
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41)
... 62 more
 
Stephan van Hulst
Saloon Keeper
Posts: 12488
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Honestly, I don't know what to tell you. It seems to be some quirk of how Spring works internally, and the workaround seems to be to simply not use fields that have the same name as the class. Which I think is good advice regardless of Spring, because what does it even mean for a class "Person" to have a field named "person"?
 
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your controller, you declared param="btnAddTest" but that's the name of the submit button. When you submit a form, the submit button isn't included in the request. What's included will be the other elements of the form, like "test" which is the name you assigned to the input text element. Spring has no way of knowing what you want to map to the test field which is why it's complaining about it.
 
Junilu Lacar
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, the params attribute of the @RequestMapping annotation is used to route requests to different handler methods based on the value of one of the request parameters. Again, "btnAddTest" will not be included in the submitted request. Maybe you really meant to use @RequestParam("test") instead to capture the value the user typed in the text input field?
 
Saloon Keeper
Posts: 22784
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:When you submit a form, the submit button isn't included in the request.



Allowing for whatever meddling Spring is doing, I think that's not completely true.

An HTML INPUT TYPE=SUBMIT won't submit the NAME= attribute, but it does submit the control's VALUE= attribute, which is the button's displayed value.


Also, has anyone noted that the actual exception is coming from the fact that the characters in the "number" to be converted are actually the value "wer"?
 
Junilu Lacar
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:
An HTML INPUT TYPE=SUBMIT won't submit the NAME= attribute, but it does submit the control's VALUE= attribute, which is the button's displayed value.


Are you sure? Because if you run this example: https://www.w3schools.com/html/tryit.asp?filename=tryhtml_form_submit the submit value is not part of the request that's sent.
 
Tim Holloway
Saloon Keeper
Posts: 22784
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that's very odd. Because long, long ago I used to do forms with multiple SUBMIT buttons and was annoyed that the SUBMIT value had to be the button label text and not just any value I wanted.

On the other hand, I just tried a form from Firefox and the URL transmitted did NOT include the SUBMIT button value. And that's not a behavior I'd expect to have changed.

It does appear that the BUTTON HTML tag can submit and send a value though.
 
Stephan van Hulst
Saloon Keeper
Posts: 12488
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
None of that explains why the exception doesn't occur when unused repository class doesn't extend CrudRepository.

Spring seems to want to convert the input string to a number (I guess because the argument of the ID type parameter of the CrudRepository is Integer), but I don't know why Spring would do this if you're not using the repository in the first place.
 
joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic