The JSON returned by springboot contains the class path of the return class

13 views Asked by At

I use springboot2.7.18 to get a JSON

This is my objectMapper configĀ file.

@Configuration
public class JackJsonConfig {
    @Value("${spring.jackson.local-date-time-format}")
    String localDateTimeFormat;
    
    @Value("${spring.jackson.local-date-format}")
    String localDateFormat;
    
    @Value("${spring.jackson.local-time-format}")
    String localTimeFormat;
    
    @Bean
    @Primary
    public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();;
        objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
            @Override
            public void serialize(Object o, JsonGenerator jsonGenerator, serializerProvider) throws IOException {
                jsonGenerator.writeString("");
            }
        });
    
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addSerializer(
            LocalDateTime.class,
            new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(localDateTimeFormat))
        );

        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(
            DateTimeFormatter.ofPattern(localDateFormat)
        ));

        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(
            DateTimeFormatter.ofPattern(localTimeFormat)
        ));

        javaTimeModule.addDeserializer(
            LocalDateTime.class,
            new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(localDateTimeFormat))
        );
    
        javaTimeModule.addDeserializer(
            LocalDate.class,
            new LocalDateDeserializer(DateTimeFormatter.ofPattern(localDateFormat))
        );
        javaTimeModule.addDeserializer(
            LocalTime.class,
            new LocalTimeDeserializer(DateTimeFormatter.ofPattern(localTimeFormat))
        );

        SimpleModule simpleModule = new SimpleModule()

        .addSerializer(Long.class, ToStringSerializer.instance)
        .addSerializer(BigDecimal.class, ToStringSerializer.instance);
    
        List<Module> moduleList = new ArrayList<>();
        moduleList.add(javaTimeModule);
        moduleList.add(simpleModule);
    
        objectMapper.registerModules(moduleList);
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    
        return objectMapper;
    }
}

Part of config in application.yaml

spring:   
 jackson:     
  local-date-time-format: yyyy/MM/dd HH:mm:ss     
  local-date-format: yyyy/MM/dd     
  local-time-format: HH:mm:ss

Part of my controller

@RequestMapping("/redis/set")     
@ResponseBody     
CommonResult<Boolean> redisSet(){         
  User user = userService.getById(1);         
  redisUtil.set("user1", user);         
  return CommonResult.success(Boolean.TRUE);    
}

my CommonResult.java define

@Data 
public class CommonResult<T> implements Serializable {     
    private static final long serialVersionUID = -2102040891517300266L;     
    private long code;     
    private int status;     
    private String message;     
    private T data;
}

I get some thing like this when I request http://localhost:8080/redis/set

[   
    "com.lybb.ms.result.CommonResult",   
    {     
      "code": 200,     
      "status": 200,    
      "message": "success",     
      "data": true   
    } 
]

I want to get this

{     
    "code": 200,     
    "status": 200,     
    "message": "success",
    "data": true  
}

What's happened, I don't want "com.lybb.ms.result.CommonResult". Please help me! Thank you very much

0

There are 0 answers