所以我有这个控制器:
@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);
}
以此作为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;
}
}
使用此请求(Chrome devtools屏幕截图,实际请求引用了属性):
我一直收到这个错误:
警告:无法写入HTTP消息:org.springframework.http.converter.HttpMessageNotWritableException:无法写入内容:没有找到序列化程序,用于类org.springframework.validation.DefaultMessageCodesResolver并且没有发现任何属性创建BeanSerializer(以避免异常,禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过引用链:org.springframework.validation.support.BindingAwareModelMap [&#34; org.springframework.validation.BindingResult.roundRangeData&#34;] - &gt; org.springframework .validation.BeanPropertyBindingResult [&#34; MessageCodesResolver的信息&#34;]);嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:没有为类org.springframework.validation.DefaultMessageCodesResolver找到序列化器,也没有发现创建BeanSerializer的属性(为了避免异常,禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过引用链:org.springframework .validation.support.BindingAwareModelMap [&#34; org.springframework.validation.BindingResult.roundRangeData&#34;] - &GT; org.springframework.validation.BeanPropertyBindingResult [&#34; MessageCodesResolver的信息&#34;])
我尝试过:
将Java对象序列化为JSON适用于响应,但我有 没有运气将JSON序列化到我控制器中的Java对象中。
答案 0 :(得分:3)
您需要从参数列表中删除属性model
。
问题是如果你为杰克逊启用FAIL_ON_EMPTY_BEANS
,你会得到一个包含大量信息的巨大回复,我相信你不需要这些信息。
我会在新的ResponseEntity中返回其他内容,比如创建自己的地图,如果这是你的愿望。
答案 1 :(得分:2)
试试这个。
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);
}
答案 2 :(得分:1)
您只需要在pojo中实现Serializble
public class RoundRangeData implements Serializable {
//code
}