首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将json对象转换为实体/joda日期

将json对象转换为实体/joda日期
EN

Stack Overflow用户
提问于 2014-03-15 17:22:14
回答 2查看 708关注 0票数 0

我有实体:

代码语言:javascript
运行
复制
@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

我的控制器接收数据:

代码语言:javascript
运行
复制
@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,有没有人有示例解决方案?

谢谢。

EN

Stack Overflow用户

发布于 2014-03-15 23:39:55

谢谢你的回复,我自己解决了这个问题,如果有人需要解决的话

我在我的模型中添加了set方法:

代码语言:javascript
运行
复制
 @JsonDeserialize(using = DeserializerJodaDateTime.class)

和DeserializerJodaDateTime.class看起来像这样:

代码语言:javascript
运行
复制
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来获得反序列化实体(例如关系)

代码语言:javascript
运行
复制
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()));
}

}
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22421908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档