我有实体:
@Entity
@Table
public class product implements Serializable{
private static final long serialVersionUID = 7166167496114624228L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@NotEmpty
@Size(max=300)
private String name;
private String description;
@Size(max=200)
private String text_small;
@Size(max=200)
@NotEmpty
private String url;
@Column
@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
private DateTime dateStart = new DateTime();
@Column
@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
private DateTime dateEnd = null;
private boolean delete = false;
private boolean status = false;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "shop_id")
@Cascade({CascadeType.ALL})
private Shop shop;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "id_type")
@Cascade({CascadeType.ALL})
private TypProduct typProduct;
@ManyToMany(mappedBy="category", fetch=FetchType.LAZY)
private Set<CategoryProduct> category = new HashSet<CategoryProduct>();
...settert and getters我的控制器接收数据:
@RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Map<String, Object> saveAjax(
@Valid @RequestBody Product product, BindingResult result) {
......
}我想通过ajax/json编辑这个实体,我使用杰克逊我有一个关于转换dateStart,dateEnd,商店,typProduct和类别的问题。@InitBinder中的Standart CustomEditor不适用于此。如何使用MappingJacksonHttpMessageConverter,有没有人有示例解决方案?
谢谢。
发布于 2014-03-15 23:39:55
谢谢你的回复,我自己解决了这个问题,如果有人需要解决的话
我在我的模型中添加了set方法:
@JsonDeserialize(using = DeserializerJodaDateTime.class)和DeserializerJodaDateTime.class看起来像这样:
public class DeserializerJodaDateTime extends JsonDeserializer<DateTime> {
@Override
public DateTime deserialize(JsonParser json, DeserializationContext arg1)
throws IOException, JsonProcessingException {
DateTimeFormatter formatDaty = DateTimeFormat.forPattern("dd/MM/yyyy");
if (json.getText().isEmpty())
return null;
return formatDaty.parseDateTime(json.getText());
}
}如果我想使用service/dao来获得反序列化实体(例如关系)
public class DeserializeShop extends JsonDeserializer<Shop> {
@Autowired
ShopService shopService;
public DeserializerShop() {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
@Override
public Shop deserialize(JsonParser json, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
return shopService.getShop(Integer.parseInt(json
.getText()));
}
}https://stackoverflow.com/questions/22421908
复制相似问题