Cannot convert from [java.lang.String] to [com.example.demo.User]
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The error Cannot convert from [java.lang.String] to [com.example.demo.User] means Java or a framework built on Java was given a string where a User object was required. The real fix is not "force the cast" but decide how a string should become a User, then implement that conversion in the right layer.
Why the Error Happens
Java does not automatically know how to turn arbitrary text into your domain object. A string and a User are unrelated types, so code like this cannot compile:
The compiler rejects it because no conversion rule exists.
Manual Parsing in Plain Java
If the string contains user data, you need to parse it into a User explicitly.
This is the fundamental idea: convert the text into fields, then construct the object.
The Spring MVC Version of the Problem
This error often appears in Spring applications when a controller expects a User, but the incoming request is only a string or a request parameter.
If the client sends JSON, the cleanest approach is to accept a request body and let Jackson deserialize it.
With this controller, the client should send JSON such as:
If the request sends a plain string instead, Spring cannot invent a User from it.
Using a Spring Converter
If your application really does want to turn a single string into a User, register a converter. This is useful for request parameters, path variables, or configuration-driven conversion.
Once Spring's conversion system knows about that rule, it can use it where string-to-object conversion is appropriate.
A Request Parameter Example
Here is a controller method that makes sense with such a converter:
Now a request parameter like ?user=Alice,30 can be converted into a User if the converter is registered.
Pick the Right Input Shape
The mistake is often conceptual rather than technical. Ask what the API is supposed to receive:
- If the client sends structured user data, use JSON and
@RequestBody. - If the client sends a simple token such as a user id, accept
StringorLongand load theUseryourself. - If a single string must map to a
User, define a SpringConverter.
These are different designs, and mixing them causes confusing binding errors.
Common Pitfalls
The biggest pitfall is trying to cast a string to User. Casting only works when the underlying object is already an instance of the target type.
Another common issue is forgetting that @RequestBody expects the request body to match the object structure. A plain query parameter or raw string is not the same thing as JSON.
Developers also write brittle string converters without validation. If the source format is name,age, the converter should reject malformed input clearly instead of throwing an obscure array or number parsing exception later.
Finally, do not overuse custom converters when the API can simply accept a DTO or JSON body. Explicit request models are usually easier to understand and maintain.
Summary
- Java cannot implicitly convert a
Stringinto a customUserobject. - In plain Java, parse the string and construct the object yourself.
- In Spring MVC, use
@RequestBodyfor JSON payloads that already represent a user. - Use a Spring
Converter<String, User>only when string-to-user conversion is truly part of the API design. - Fix the data contract first, then implement the conversion at the correct layer.
Related reading
- Cannot create a reference to an object with a NULL id mongo hibernate-mongo and spring boot
- Cannot create namespace in multi-document transactionMongoDB 4.0, Spring Data 2.1.0, Spring Boot
- Cannot deserialize value of type java.util.Date from String
- Cannot disable Spring Cloud Config via spring.cloud.config.enabledfalse
- Cannot find MessageMatcherDelegatingAuthorizationManager class while trying to configure websocket security in Spring Boot 3
- Cannot get maven project.version property in a Spring application with Value
- Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6
- Cannot invoke org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getPatterns because this.condition is null

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.