Spring boot 无法写入JSON:JsonObject;嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:JsonObject

Spring boot 无法写入JSON:JsonObject;嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:JsonObject,spring-boot,gson,Spring Boot,Gson,弹簧靴2.5 @PostMapping("/cart/product") public Response addProduct(@RequestBody Map<String, Object> payloadMap) { logger.info("addProduct: payloadMap: " + payloadMap); String userName = payloadMap.get("user_name").toString(

弹簧靴2.5

    @PostMapping("/cart/product")
    public Response addProduct(@RequestBody Map<String, Object> payloadMap) {
        logger.info("addProduct: payloadMap: " + payloadMap);
        String userName = payloadMap.get("user_name").toString();
        final Product product = new ObjectMapper().convertValue(payloadMap.get("product"), Product.class);
        int quantity = (int) payloadMap.get("quantity");
        Cart findCart = cartRepository.findByUsername(userName);
        if (findCart == null) {
            Cart cart = new Cart();
            cart.setCreated(new Date());
            cart.setUsername(userName);
            cart.addProduct(product, quantity);
            cartRepository.save(cart);
            logger.info("addProduct: success_add_product_to_new_cart: " + cart);
            return ResponseService.getSuccessResponse(GsonUtil.gson.toJsonTree(cart));
        } else {
            findCart.addProduct(product, quantity);
            logger.info("addProduct: before_save_exist_cart: " + findCart);
            cartRepository.save(findCart);
            logger.info("addProduct: success_add_product_to_exist_cart: " + findCart);
            return ResponseService.getSuccessResponse(GsonUtil.gson.toJsonTree(findCart));
        }
    }


public class ResponseService {
    private static final int SUCCESS_CODE = 0;
    private static final String SUCCESS_MESSAGE = "Success";
    private static final int ERROR_CODE = -1;

    private static Logger logger = LogManager.getLogger(ResponseService.class);

    public static Response getSuccessResponse(JsonElement body) {
        Response response = new Response(SUCCESS_CODE, SUCCESS_MESSAGE, body);
        logger.info("getSuccessResponse: response = " + response);
        return response;
    }


import com.google.gson.JsonElement;

public class Response {
    private int code;
    private String message;
    private JsonElement body;

    public Response(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public Response(int code, String message, JsonElement body) {
        this.code = code;
        this.message = message;
        this.body = body;
    }

SpringBoot默认使用jackson序列化json。在响应对象中,您有字段
JsonElement body
。它是来自gson包的对象,jackson不知道如何序列化它

解决方案:添加属性(在
应用程序.properties
文件中)以使用gson而不是jackson。请注意,Spring启动版本很重要

弹簧启动>=2.3.0。释放:
spring.mvc.converters.preferred json mapper=gson

弹簧防尘套<2.3.0。释放:
spring.http.converters.preferred json mapper=gson

更多信息:

Application.properties

spring.mvc.converters.preferred-json-mapper=gson

我找到了一个解决办法,保留Jackson,但为导致Jackson序列化问题的类实现我自己的序列化程序

这是一个很粗糙的解决方案,但它现在起作用了

public class GSONObjectSerializer extends SimpleSerializers {

private static final long serialVersionUID = -8745250727467996655L;

private class EmptySerializer extends StdSerializer<Object> {
    
    /**
     * 
     */
    private static final long serialVersionUID = 5435498165882848947L;

    protected EmptySerializer(Class t) {
        super(t);
    }

    @Override
    public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        // --> Will write an empty JSON string
        gen.writeStartObject();
        gen.writeEndObject();
    }
}

@Override
public JsonSerializer<?> findSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc) {

    // --> Here is to setup which class will be serialised by the custom serializer.
    if (type.isTypeOrSubTypeOf(JsonObject.class) || type.isTypeOrSubTypeOf(StripeResponse.class)) {
        return new EmptySerializer(type.getRawClass());
    }
    return super.findSerializer(config, type, beanDesc);
}

如果我们仍然想使用Jackson而忽略JsonObject怎么办?@nano您可以删除JsonObject字段或更改字段类型-您可以尝试从Jackson包中删除JsonNode。返回对象由第三方库提供,因此,我无法删除该字段。
public class GSONObjectSerializer extends SimpleSerializers {

private static final long serialVersionUID = -8745250727467996655L;

private class EmptySerializer extends StdSerializer<Object> {
    
    /**
     * 
     */
    private static final long serialVersionUID = 5435498165882848947L;

    protected EmptySerializer(Class t) {
        super(t);
    }

    @Override
    public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        // --> Will write an empty JSON string
        gen.writeStartObject();
        gen.writeEndObject();
    }
}

@Override
public JsonSerializer<?> findSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc) {

    // --> Here is to setup which class will be serialised by the custom serializer.
    if (type.isTypeOrSubTypeOf(JsonObject.class) || type.isTypeOrSubTypeOf(StripeResponse.class)) {
        return new EmptySerializer(type.getRawClass());
    }
    return super.findSerializer(config, type, beanDesc);
}
@Configuration
public class SerialisationConfig {
    @Bean
    public ObjectMapper createMapper() {
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.setSerializers(new GSONObjectSerializer());
        return  Jackson2ObjectMapperBuilder.json().modules(Arrays.asList(simpleModule)).build();
    }
}