Can't make Jackson and Lombok work together

后端 未结 14 1359
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 13:48

I am experimenting in combining Jackson and Lombok. Those are my classes:

package testelombok;

import com.fasterxml         


        
14条回答
  •  难免孤独
    2020-11-27 14:23

    Immutable + Lombok + Jackson can be achieved in next way:

    import com.fasterxml.jackson.databind.ObjectMapper;
    import lombok.AccessLevel;
    import lombok.AllArgsConstructor;
    import lombok.NoArgsConstructor;
    import lombok.Value;
    
    @Value
    @NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
    @AllArgsConstructor
    public class LocationDto {
    
        double longitude;
        double latitude;
    }
    
    class ImmutableWithLombok {
    
        public static void main(String[] args) throws Exception {
            ObjectMapper objectMapper = new ObjectMapper();
    
            String stringJsonRepresentation = objectMapper.writeValueAsString(new LocationDto(22.11, 33.33));
            System.out.println(stringJsonRepresentation);
    
            LocationDto locationDto = objectMapper.readValue(stringJsonRepresentation, LocationDto.class);
            System.out.println(locationDto);
        }
    }
    

提交回复
热议问题