Spring Boot Admin 注册错误

Spring Boot Admin registering error

我在尝试注册应用程序时在服务器日志中看到这个:

Failed to read HTTP message:  org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not construct instance of de.codecentric.boot.admin.model.Application: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of de.codecentric.boot.admin.model.Application: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: java.io.PushbackInputStream@765cecc4; line: 1, column: 2]

这可能是什么问题?

我正在使用 Spring Boot Admin v1.5.7

我和你有同样的问题... 我想通了原因。 这是我的解决方案:

        @Configuration
    public class JacksonConfiguration {
        @Bean
        @Primary
        public ObjectMapper jacksonObjectMapper() {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
            SimpleModule simpleModule = new SimpleModule("SimpleModule",
                    Version.unknownVersion());
            simpleModule.addSerializer(JsonResult.class, new CustomJsonResultSerializer());
            simpleModule.addDeserializer(Application.class, new ApplicationDeserializer());
            objectMapper.registerModule(simpleModule);
            objectMapper.setInjectableValues(new IgnoreNullFieldInjectableValues());
            return objectMapper;
        }
}

addDeserializer(Application.class, new ApplicationDeserializer());

de.codecentric.boot.admin.jackson.ApplicationDeserializer de.codecentric.boot.admin.model.Application

您必须使用 ApplicationDeserializer 来反序列化模型 Application

这对我有用我收到错误:

:29:47.072 [http-nio-8088-exec-5] WARN org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON document: Can not construct instance of de.codecentric.boot.admin.model.Application: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?) at [Source: java.io.PushbackInputStream@30d871a4; line: 1, column: 2]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of de.codecentric.boot.admin.model.Application: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?) at [Source: java.io.PushbackInputStream@30d871a4; line: 1, column: 2]

@Configuration
    public class JacksonConfiguration {


    @Bean
    @Primary
    public ObjectMapper jacksonObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
        SimpleModule simpleModule = new SimpleModule("SimpleModule", Version.unknownVersion());
        simpleModule.addDeserializer(Application.class, new ApplicationDeserializer());
        objectMapper.registerModule(simpleModule);
        return objectMapper;
    }
}

这个问题的解决方案是扩展 AbstractHttpMessageConverter:

public class MappingJackson2String2HttpMessageConverter extends AbstractHttpMessageConverter<Object> {
    //...
    @Override
    protected boolean supports(Class<?> clazz) {
        return supportedClasses.contains(clazz);
    }

    @Override
    protected boolean canWrite(MediaType mediaType) {
        return mediaType == null || mediaType.toString().contains(MediaType.APPLICATION_JSON_VALUE);
    }

    @Override
    protected String readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
        Charset charset = getContentTypeCharset(inputMessage.getHeaders().getContentType());
        return StreamUtils.copyToString(inputMessage.getBody(), charset);
    }

    @Override
    protected void writeInternal(Object linkedHashMap, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
        outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets());
        Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType());
        String result = linkedHashMap instanceof String ? (String) linkedHashMap : objectMapper.writeValueAsString(linkedHashMap);
        StreamUtils.copy(result, charset, outputMessage.getBody());
    }
    //...
}

并在应用程序配置中将其添加为消息转换器:

@Configuration
public class BackendConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new MappingJackson2String2HttpMessageConverter());
        //...
    }
}

因为我收到的加密负载是 plain/text 内容类型。