Spring - Problem converting a pojo to a JSON (No converter found error)

no converter found for return value of type spring mvc json
how to send json response from spring controller
no converter found for return value of type cl
httpmessageconverter
abstracthttpmessageconverter
how to send json object in spring boot
spring boot json converter
convert responseentity to json object

spring 4.3.3

I'm trying to convert a Pojo to JSON, marking the Controller as a @RestController, the problem with that is some elements are with first letter lower instead of upper case,

Ex:
"Id": 1, //This is ok  
"customerId": "1234", //Instead of CustomerId, this has customerId
...

Controller

@RestController
...
public class CustomerController{
    ...
    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public CustomerResponse postCustomerRequest(final HttpServletRequest request) { 

I want that to be in upper case. The pojo is basically a xjc generated class from an xsd, and it contains,

@XmlElement(name = "Id")
protected int id;
@XmlElement(name = "CustomerId")
protected String customerId;
...
    public int getId() {
        return id;
    }
    public void setId(int value) {
        this.id = value;
    }
    public String getCustomerId() {
        return customerId;
    }
    public void setCustomerId(String value) {
        this.customerId = value;
    }

This has associated setter, getter for each attributes. In the Controller, I got ObjectMapper case insensitive set to true as well,

mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true); 

I also tried with, marking the Controller as @Controller instead of @RestController, provided @ResponseBody before the method,

Controller

@Controller
...
public class CustomerController {
...
    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    @ResponseBody
    public String postCustomerRequest(HttpServletRequest request) {
        ...

//Used PropertyNamingStrategy with the ObjectMapper, converted the first character to an upper case, 
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
            ...
            CustomerResponse response=createCustomer(document,objectFactory);
            mapperObj.setPropertyNamingStrategy(new CustomerJsonNameStrategy());
            String jsonOutput = mapperObj.writeValueAsString(response);
            return jsonOutput;

If I see the value of jsonOutput during debug in Eclipse, it outputs the json elements in correct case, but the response to the rest client is coming as,

{"errors": [{
   "message": "No converter found for return value of type: class java.lang.String",
   "type": "IllegalArgumentError"
}]}

Looks like the jackson serializier is interferring with the response and throwing the above error.

What's the solution for this?

Spring uses Jackson for converting POJO to json. Jackson by default makes first letter of field name small. If you wants custom names then add following jackson annotation.

@JsonProperty("CustomerId")
private String customerId; 

Spring Boot RestController Error: “No Converter Found for Return , happens and how public getters are one possible solution to the problem. Spring Boot RestController Error: “No Converter Found for Return Value of Type” from a mapped request method, and it is converted into a JSON result. For each property in your POJO that you want returned in your JSON,  Spring Boot RestController Error: “No converter found for return value of type” Spring Boot RestControllers by default can return a Pojo class as the return result from a mapped request method, and it is converted into a Json result.

@Consumes does not help you very much in this case. It should be used to specify format of the input. Check this article: http://www.javainterviewpoint.com/jax-rs-rest-consumes-example/

If you can see the jsonOutput during debug, your problem may be related to the way your client expects the output : your client may expect a JSON instead of a String object. Solution : return CustomerResponse instead of String.

@RestController
...
public class CustomerController {
  ...
  public CustomerResponse postCustomerRequest(final HttpServletRequest request) 
{

and CustomerResponse should have the fields annotated with @JsonProperty if you want to change the default naming.

@JsonProperty(name = "Id")
protected int id;

@JsonProperty(name = "CustomerId")
protected String customerId;

Returning JSON object as response in Spring Boot when returning , When using Spring Boot @RestController, we are able to return directly an Entity in json format, "TEXT") @Convert(converter= JSONObjectConverter.class) private JSONObject jsonData; }. Results in an error similar to the one below: InvalidDefinitionException: No serializer found for class org.json. Spring Boot RestController Error: “No Converter Found for Return Value of Type” Let's take a look at how this happens and how public getters are one possible solution to the problem. by

I would configure the MappingJackson2HttpMessageConverter with your custom property naming strategy, which is in IMHO the cleaner solution compared to serializing to json in every controller method

<mvc:annotation-driven>
    <mvc:message-converters>
        ...
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper" ref="objectMapper"/>
        </bean>
        ...
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

<bean id="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
    <property name="propertyNamingStrategy" ref="customNamingStrategy" />
</bean

Or, you make sure that the StringHttpMessageConverter is registered as converter in your spring MVC config, if you really want or need to control the response body

<mvc:annotation-driven>
     <mvc:message-converters>
            ...
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            ...
   </mvc:message-converters>
</mvc:annotation-driven>

Returning JSON object as response in Spring Boot, As you are using Spring Boot web, Jackson dependency is implicit and we do not Just return a POJO and jackson serializer will take care of converting to json The problem you are facing is because the returned object(JSONObject) StringHttpMessageConverter instead of JSON converter in this case. As payload, we set the JSON String representation of a Person. The fromMessage() method converts the JSON String from a JMS message into a Person. The conversion between a Java object and JSON is done using a Jackson ObjectMapper. Annotate the class with @Component so that Spring registers the class as a Bean

I made it to work by moving to an extension which doesn't have jackson setup. PS: This is in SAP Hybris and it contains multiple extensions/projects.

I marked the Controller with @Controller, added @Produces({MediaType.APPLICATION_JSON}), removed @ResponseBody, set the method return type as ResponseEntity, used a PropertyNamingStrategy to convert first letter in upper case.

public ResponseEntity<String> postCustomerRequest(final HttpServletRequest request) {
    ...
    final org.codehaus.jackson.map.ObjectMapper mapperObj = new org.codehaus.jackson.map.ObjectMapper();
    CustomerResponse response=createCustomer(document,objectFactory);
    mapperObj.setPropertyNamingStrategy(new CustomerJsonNameStrategy());

    final HttpHeaders httpHeaders= new HttpHeaders();
    httpHeaders.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);
    return new ResponseEntity<String>(mapperObj.writeValueAsString(response), httpHeaders, HttpStatus.OK);

}

Http Message Converters with the Spring Framework, Resource for any type of octet stream; SourceHttpMessageConverter It will then try to find a registered converter that's capable of handling that specific The process is similar for receiving a request which contains JSON information. It will then search for a HttpMessageConverter that can convert the  hi all, I am getting the below error: org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org

Jackson, The article is focused on showing how to use the new JSON-P support in Spring 4.1. Read more →. 2. The Problem. shivmaruti singh wrote:anyone can suggest me how to convert given json into pojo in controller please its urgent. People answering here are from different time zones, so please be patient "Any fool can write code that a computer can understand.

Spring boot return empty json, Using the JDBC core classes to control basic JDBC processing and error handling To overcome such problems Maven supports the concept of a "bill of materials" You should find that your Spring application works and logs happily to the Notice that there is nothing special about this class, it is a POJO that has no  Jackson is pretty much straight forward in converting between simple pojo objects. Typically it involves only two steps, Create instance of com.fasterxml.jackson.databind.ObjectMapper. Use objectMapper.writeValueAsString () method to convert pojo to JSON. Employee emp = new Employee (1, "Lokesh Gupta", 34, "India"); //Object mapper instance.

Spring Framework Reference Documentation, spring - Getting error "No converter found capable of converting from java spring spring-mvc spring-boot. 1. No converter found for return value of type: class java.lang. objectMapper = new ObjectMapper(); String json = objectMapper. So, to solve this issues you can implement own converter (​Converter) that gets  You need to decorate your rest resource with @Produces({MediaType.APPLICATION_JSON}). Below is an example resource that only uses JSON. You could include other media types, such as xml, and Jersey would automatically use the appropriate media type based on the Accepts header sent by the client.