• 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
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

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

 
Greenhorn
Posts: 20
  • 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: 12501
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: 20
  • 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: 12501
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us the entire stack trace?
 
Jf Okeeffe
Greenhorn
Posts: 20
  • 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: 12501
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: 15999
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: 15999
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: 22807
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: 15999
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: 22807
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: 12501
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.
 
Jf Okeeffe
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote: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"?



I agree that in this "Person" example doesn't make much sense. But there are cases it does make sense. For instance, if you have a class called "Language" with an attribute that is also called "language" (english, spanish...) and an attribute "profficiency" (basic, fluent...).
 
Junilu Lacar
Sheriff
Posts: 15999
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jf Okeeffe wrote:... there are cases it does make sense. For instance, if you have a class called "Language" with an attribute that is also called "language" (english, spanish...) and an attribute "profficiency" (basic, fluent...).


I don't think that makes any sense either. Why would a language have a "language" attribute? And how can a language have a proficiency attribute? English can't be "basic" in itself.

I would tell a different story, like "A person might know English and have a basic level of proficiency in it." Given this, I would argue that the idea you're trying to capture is not "Language" but rather "Fluency." In code, that might be expressed as something like this:
 
Jf Okeeffe
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:English can't be "basic" in itself.



Sorry if I was not clear.
I didn't mean the profficiency of english being basic in itself.
I meant the profficienty of a person in that specific language being basic.
My example is of a language class associated with a person class (just like you suggested) where the language class would represent all of the languages a person is skilled at and the persons profficiency level (basic, fluent...) on each one.

Anyway, even if you don't like my example (and it might not be one of the best indeed) I think we can't presume that needing to have an attribute with the class name would never be necessary. Someone somewhere might need it. Just like I needed it. And in fact, I'd say this is pretty common to be done as java (without spring crud repository) allows it. So I believe would be good if spring allowed it as well letting the developer decide how it would like to do.
 
Tim Holloway
Saloon Keeper
Posts: 22807
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not totally folllowing all this, but Spring operates according to POJO/JavaBean principals. That includes encoding for classes and properties.

A class name MUST start with a capital letter. A property name MUST NOT start with a capital letter. There is no ambiguity. Having a property within a class that has the same name as the class is not an issue. Spring will likely mis-handle things if the capitalization conventions are not properly observed. More accurately, the BeanUtils library that Spring employs might be offended. But class names and class properties also exist in separate namespaces and so it's perfectly legal in Java to actually have 2 properties named "Person" and "person" in a class named "Person". Spring won't like it, and neither will I, but the Java compiler and runtimes won't care.

Having a property named "person" within class Person is totally OK with Spring, though, and I've probably even got some classes with similar constructs if I were to look around. The context makes it obvious to Spring what's what.
 
Jf Okeeffe
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:Having a property named "person" within class Person is totally OK with Spring, though...



Not really. That is exactly what I pointed out in the original post here.

See where I said:

Jf Okeeffe wrote: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).



But that error only happens when using CrudRepository as I also pointed out in the original post.
When not using it, Spring doesn't complain as you said.
 
Tim Holloway
Saloon Keeper
Posts: 22807
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't follow. Certainly an EL expression of "person.person" is 100% valid if you're following Spring/POJO standards.

If you're thinking it doesn't because of failure to convert datatypes, that's not Spring's fault. It's the fault of how you configured things. Spring cannot determine if you're attempting to cram a square peg in a round hole at compile-time in most cases.
 
Jf Okeeffe
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To understand you would need to reproduce the error I refer to in the original post here.
There is a link there to download the code.
 
Rancher
Posts: 4749
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Out of curiosity, and because it would be the first thing I check, have you looked at what is actually sent from the page, using the browser dev tools?

Edit: I ask because I find it hard to believe that Spring has made up this value "wer" out of whole cloth.
 
Jf Okeeffe
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"wer" is the content I filled in the input box and then clicked save.
Could have filled with anything else.
 
Tim Holloway
Saloon Keeper
Posts: 22807
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jf Okeeffe wrote:"wer" is the content I filled in the input box and then clicked save.
Could have filled with anything else.


Not if you wanted to assign it to a numeric property you couldn't.

And that's what you were doing with "wer".
 
Jf Okeeffe
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Because like I said, if I change the attribute name of the class to a name that is different than the class name the error will not occur. Even if I input a string in that field.
And if you take a look at the code the field is a string (private String test), not an int.
Looks like spring is doing that type of conversion in the backstage somehow when using CrudRepository.
I'm nowhere assigning that content to an int. Spring seems to be doing it.
 
Tim Holloway
Saloon Keeper
Posts: 22807
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're defining a Repository for an entity type if Test with an identified type of "Integer". The operation is failing because you've told Spring to jam a non-numeric value ("wer") into the numeric CRUDRepository ID.

I think you're mixing JPA constructs with stuff that's non-JPA, but that's outside of what I've worked with lately. Still, I have doubts about that @ID annotation in this context considering that CRUDRepository has its own OID considerations.
 
Jf Okeeffe
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:The operation is failing because you've told Spring to jam a non-numeric value ("wer") into the numeric CRUDRepository ID.



If that is the case why is it that it works (with any string such as "wer") if I change the attribute "test" to a name that is different than the class name?
It would be just a change in the attribute name, not a change on types.
 
Tim Holloway
Saloon Keeper
Posts: 22807
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can make only a rough guess, especially since you never did post the actual bad source code here. Sorry, I'm not up to pulling down your project. Besides not being paid to do this, I'm not a Spring MVC expert. I use JavaServer Faces. And in fact, have hosted the forum for that framework for many years.

But it's obvious that what you think you're telling it to do isn't what you're actually telling it to do. You appear to be attempting to create a record that has a numeric key from a form that doesn't have a numeric key. So it's no surprise that the framework is throwing an exception.
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic