2

尝试注册应用程序时,我在服务器日志上得到了这个:

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

4

3 回答 3

0

我和你有同样的问题……我找到了原因。这是我的解决方案:

        @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

于 2018-06-05T07:20:04.787 回答
0

这对我有用,我收到错误:

:29:47.072 [http-nio-8088-exec-5] 警告 org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - 无法读取 HTTP 消息:org.springframework.http.converter.HttpMessageNotReadableException:无法读取 JSON文档:无法构造 de.codecentric.boot.admin.model.Application 的实例:找不到合适的构造函数,无法从 Object 值反序列化(缺少默认构造函数或创建者,或者可能需要添加/启用类型信息?)在 [来源:java.io.PushbackInputStream@30d871a4;行:1,列:2];嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException:无法构造 de.codecentric.boot.admin.model.Application 的实例:找不到合适的构造函数,无法从 Object 值反序列化(缺少默认构造函数或创建者,或者可能需要添加/启用类型信息?)在 [Source: java.io.PushbackInputStream@30d871a4; 行:1,列: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;
    }
}
于 2018-08-28T06:52:08.783 回答
0

解决方案是扩展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内容类型。

于 2018-10-11T23:14:42.903 回答