티스토리 뷰

1. 요청사항

- API 호출 했을 때 결과 값이 null 인 경우 ""값 치환 

- Float, Double, Long, Integer 등 숫자 타입인 경우 문자 형태로 반환, 100 -> "100"

2. 환경

- spring boot : 2.2.6.RELEASE

- java 1.8

- gradle-6.3

3. 작업 내용 

- NullSerializer 생성 : null 값 치환

- NumberToStringSerializer 생성 : 숫자 값 치환 

- CustomObjectMapper 생성

- WebMvcConfigurationSupport 구현 시 CustomObjectMapper를 Converter에 등록.

 

NullSerializer.java

public class NullSerializer extends JsonSerializer<Object> {
    public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)
            throws IOException, JsonProcessingException {
        jgen.writeString("");
    }
}

NumberToStringSerializer.java

public class NumberToStringSerializer extends JsonSerializer<Number> {

    @Override
    public void serialize(Number value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if(value == null) gen.writeString("");
        else gen.writeString(value.toString());
    }

}

CustomObjectMapper.java

public class CustomObjectMapper extends ObjectMapper {
    
    private static final long serialVersionUID = -2148669317097583174L;

    public CustomObjectMapper(){
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Number.class, new NumberToStringSerializer());
        registerModule(simpleModule);
        getSerializerProvider().setNullValueSerializer(new NullSerializer());
        configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // 없는 필드로 인한 오류 무시 
    }
}

WebMvcConfiguration.java

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    public WebMvcConfiguration() {
        super();
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) 
    {
        converters.add(new MappingJackson2HttpMessageConverter(customObjectMapper())); 
    }
    
    @Bean
    public ObjectMapper customObjectMapper() {
        return new CustomObjectMapper();
    }

}

4. Test  

User.java

public class User {

    private Integer age1;
    private Long age2;
    private Float age3;
    private Double age4;
    private BigDecimal age5;
    private String id;
    private String name;
    public Integer getAge1() {
        return age1;
    }
    public void setAge1(Integer age1) {
        this.age1 = age1;
    }
    public Long getAge2() {
        return age2;
    }
    public void setAge2(Long age2) {
        this.age2 = age2;
    }
    public Float getAge3() {
        return age3;
    }
    public void setAge3(Float age3) {
        this.age3 = age3;
    }
    public Double getAge4() {
        return age4;
    }
    public void setAge4(Double age4) {
        this.age4 = age4;
    }
    public BigDecimal getAge5() {
        return age5;
    }
    public void setAge5(BigDecimal age5) {
        this.age5 = age5;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "User [age1=" + age1 + ", age2=" + age2 + ", age3=" + age3 + ", age4=" + age4 + ", age5=" + age5
                + ", id=" + id + ", name=" + name + "]";
    }
    
}

TestController.java

@RestController
public class TestController {

    
    @GetMapping("/test")
    public User test(User pUser) {
        
        return pUser;
    }
}

요청

- http://localhost:8070/test

결과

-  {"age1":"","age2":"","age3":"","age4":"","age5":"","id":"","name":""}

빈 값인 경우 숫자, 문자열이 모두 빈값으로 반환된다. 

 

요청

- http://localhost:8070/test?age1=1001&age2=1002&age5=1005&id=kkaok

결과

- {"age1":"1001","age2":"1002","age3":"","age4":"","age5":"1005","id":"kkaok","name":""}

숫자 타입도 문자로 반환 된다. 

 

 

GitHub : https://github.com/kkaok/examples/tree/master/NullSerializerExample

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함