杰克逊@Deserializer不起作用

时间:2014-06-06 06:18:31

标签: jackson deserialization

这是this

的重复问题

POJO

@Entity
@Table(name = "pet")
public class Pet {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long petId;

    private String petName;

    private Date birthday;

    @JsonSerialize(using = Serializer.class)
    public Date getBirthday() {
        return birthday;
    }

    @JsonDeserialize(using = Deserializer.class)
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Long getPetId() {
        return petId;
    }

    public void setPetId(Long petId) {
        this.petId = petId;
    }

    public String getPetName() {
        return petName;
    }

    public void setPetName(String petName) {
        this.petName = petName;
    }
}

串行

public class Serializer extends JsonSerializer<Date> {
    @Override
    public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        System.out.println("Serializer");
    }
} 

解串器

public class Deserializer extends JsonDeserializer<Date> {
    @Override
    public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        System.out.println("Deserializer works.");
        return new Date();
    }
}

Maven依赖

<dependency> 
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.3.2</version>
  </dependency>
  <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.3.2</version>
  </dependency>
  <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.3.2</version>
  </dependency>

更新:控制器代码

@Transactional
@Controller
public class Demo {

    @Resource
    private Dao dao;

    @ResponseBody
    @RequestMapping("/test")
    public String tester(Pet pet){
        dao.saveOrUpdate(pet);
        return "success";
    }

    @ResponseBody
    @RequestMapping("/test2")
    public List<Pet> tester2(){
        List<Pet> pets = dao.query(Pet.class);
        return pets;
    }
}

但是每次只会调用序列化程序反序列化程序不会产生任何影响,我看不出哪里出错,有人可以帮助我吗?

感谢。

0 个答案:

没有答案