序列化器仅来自指定控制器的方法-Spring,JSON Jackson

问题描述:

我有2个控制器和一个自定义序列化字段(如@JsonSerialize(using = MySerialization.class))的get方法.

I have 2 controllers and a get method for a field with custom serialization like as @JsonSerialize(using = MySerialization.class).

但是我只想在从A Controller而不是从B Controller调用方法时进行此序列化.

But I want to make this serialization just when I call method from A Controller, not from B Controller.

我该如何指定呢?

好的,我假设您具有以下请求映射

Okay, I will assume that you have the follow requestmaps

@RequestMapping(value = "/saveA", method = RequestMethod.GET)
public @ResponseBody Person getPersonA() {
    return getPerson();
}

@RequestMapping(value = "/saveB", method = RequestMethod.GET)
public @ResponseBody Person getPersonB() {
    return getPerson();
}

private Person getPerson() {
    return new Person("Elvis");
}

static class Person {

    private String name;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }
}

所以您想在每个requestMap上以不同的方式序列化 Person 对象,我个人找不到(甚至看不到)Spring解决方案,我认为这是一个Jackson和Java问题得到解决,所以这是我的解决方案:

So you want to serialize the Person object in different ways at each requestMap, I personally could not found (even see) a Spring Solution for that, I think that is a Jackson and Java problem to be solved, so here is my solution:

创建一个 Person 子类,然后根据需要对其进行自定义,例如

Create a Person subclass then customize it as you need, for example

static class CustomPerson extends Person {

    public CustomPerson(String name) {
        super(name);
    }

    @JsonSerialize(using = NameSerializer.class)
    @Override
    public String getName() {
        return super.getName();
    }
}

static class NameSerializer extends JsonSerializer {

    @Override
    public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeString("customSerializer-" + String.valueOf(value));
    }
}

然后,您要做的就是创建一个映射器方法,该方法将您的 Person 转换为 CustomPerson

Then, all you need to do is create a mapper method, that converts your Person to a CustomPerson

@RequestMapping(value = "/saveB", method = RequestMethod.GET)
public @ResponseBody Person getPersonB() {
    return getCustomPerson();
}

private Person getCustomPerson() {
    return new CustomPerson(getPerson().getName());
}

另一种选择是您自己创建对象映射器,并在需要定制对象时序列化所需的对象

Te another option is to youself create the object mapper and serialize the Object as you want when you need it customized

@RequestMapping(value = "/saveC", method = RequestMethod.GET)
public void getPersonC(HttpServletResponse response) throws IOException {
    response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
    new ObjectMapper()
    .registerModule(new SimpleModule().addSerializer(Person.class, new JsonSerializer<Person>() {
        @Override
        public void serialize(Person value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            gen.writeStartObject();
            gen.writeStringField("name", "Custom-" + value.getName());
            gen.writeEndObject();
        }
    }))
    .writeValue(response.getWriter(), getPerson());
}