Reputation: 61
So I have this controller:
@RequestMapping(value = "/zone/settings/ranged/update", method = RequestMethod.POST, produces="application/json", consumes="application/json")
public @ResponseBody ResponseEntity<Map<String, Object>> zoneSettingsRangedUpdate(
WebRequest request,
@RequestBody RoundRangeData data,
Map<String, Object> model
) throws IOException {
model.put("request", data);
model.put("success",true);
return new ResponseEntity<>(model, HttpStatus.OK);
}
With this as the POJO:
public class RoundRangeData {
private BigInteger pricingDate;
private Long zoneid;
private Long psid;
private Float roundTo;
private Float lowerLimit;
private Float upperLimit;
public RoundRangeData() {
}
public RoundRangeData(BigInteger pricingDate, Long zoneid, Long psid, Float roundTo, Float lowerLimit, Float upperLimit) {
this.pricingDate = pricingDate;
this.zoneid = zoneid;
this.psid = psid;
this.roundTo = roundTo;
this.lowerLimit = lowerLimit;
this.upperLimit = upperLimit;
}
public BigInteger getPricingDate() {
return pricingDate;
}
public void setPricingDate(BigInteger pricingDate) {
this.pricingDate = pricingDate;
}
public Long getZoneid() {
return zoneid;
}
public void setZoneid(Long zoneId) {
this.zoneid = zoneId;
}
public Long getPsid() {
return psid;
}
public void setPsid(Long psid) {
this.psid = psid;
}
public Float getRoundTo() {
return roundTo;
}
public void setRoundTo(Float roundTo) {
this.roundTo = roundTo;
}
public Float getLowerLimit() {
return lowerLimit;
}
public void setLowerLimit(Float lowerLimit) {
this.lowerLimit = lowerLimit;
}
public Float getUpperLimit() {
return upperLimit;
}
public void setUpperLimit(Float upperLimit) {
this.upperLimit = upperLimit;
}
}
Using this request (Chrome devtools screenshot, actual request has quotes around properties):
And I keep getting this error:
WARNING: Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: No serializer found for class org.springframework.validation.DefaultMessageCodesResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.validation.support.BindingAwareModelMap["org.springframework.validation.BindingResult.roundRangeData"]->org.springframework.validation.BeanPropertyBindingResult["messageCodesResolver"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.springframework.validation.DefaultMessageCodesResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.validation.support.BindingAwareModelMap["org.springframework.validation.BindingResult.roundRangeData"]->org.springframework.validation.BeanPropertyBindingResult["messageCodesResolver"])
What I have tried:
Serializing Java objects into JSON works for responses, but I'm having no luck serializing JSON into Java objects in my controllers.
Upvotes: 1
Views: 4518
Reputation: 8606
You need to remove the attribute model
from the parameter list.
The problem if you enable FAIL_ON_EMPTY_BEANS
for jackson, you will get a huge response back which contain a lot of information, which I am pretty sure you do not need.
I would return in the new ResponseEntity something else, like creating your own map, if that is your desire.
Upvotes: 3
Reputation: 1148
Try this.
public class MapSerializer extends JsonSerializer<SpecialMap> {
@Override
public void serialize(SpecialMap map, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
jgen.writeStartObject();
for (String key : map.keySet()) {
jgen.writeStringField(key, map.get(key));
}
jgen.writeEndObject();
}
}
public class SpecialMap extends HashMap<String,String> {
}
@RequestMapping(value = "/zone/settings/ranged/update", method = {RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, Object>> zoneSettingsRangedUpdate(
@RequestBody RoundRangeData data
) throws IOException {
Map<String, Object> model = new HashMap<>();
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(SpecialMap.class, new MapSerializer());
mapper.registerModule(module);
String serialized = mapper.writeValueAsString(data);
// return serialized;
model.put("request", serialized);
model.put("success",true);
return new ResponseEntity<Map<String, Object>>(model, HttpStatus.OK);
}
See the image of successful response.
Upvotes: 2
Reputation: 5948
You only need to implement Serializble in your pojo
public class RoundRangeData implements Serializable {
//code
}
Upvotes: 1