Spring Boot报错No converter found for return type问题求助
Hey! I’ve run into this exact issue plenty of times, and even though Spring Boot ships with Jackson (its default JSON converter) out of the box, there’s a straightforward reason you’re hitting this error—your Response class isn’t set up properly for serialization.
Why this happens
Jackson needs a way to access the properties in your Response class to turn it into JSON. From the code snippet you shared, you only have a setCode method but no matching getter, and your fields are private (the default when you don’t specify a modifier). Without getters, public fields, or serialization annotations, Jackson can’t read the values you’ve set on the object.
Solutions to fix this
Here are the most common (and cleanest) ways to resolve the error:
1. Add getter methods for all fields (Recommended)
Stick to standard Java encapsulation by adding getters for your code and message fields. This is the best practice for maintainable code:
public class Response { String code; String message; // Your existing setters public void setCode(String code) { this.code = code; } public void setMessage(String message) { this.message = message; } // Add these getters public String getCode() { return this.code; } public String getMessage() { return this.message; } }
2. Make fields public (Quick workaround)
If you want a faster fix (though less ideal for encapsulation), you can mark your fields as public so Jackson can access them directly:
public class Response { public String code; public String message; // You can keep your setters or assign values directly to the fields }
3. Use Jackson annotations (Alternative)
If you don’t want to add getters, you can use Jackson’s @JsonProperty annotation to explicitly tell the converter to serialize your private fields:
import com.fasterxml.jackson.annotation.JsonProperty; public class Response { @JsonProperty String code; @JsonProperty String message; public void setCode(String code) { this.code = code; } public void setMessage(String message) { this.message = message; } }
Once you apply any of these changes, Spring should be able to serialize your Response object to JSON without throwing that converter error.
内容的提问来源于stack exchange,提问作者Vish K

