Cannot convert from [java.lang.String] to [com.example.demo.User]
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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.

