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?
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);
}