Search code examples
spring-boot-admin

Spring Boot Admin registering error


I get this on server log when trying to register an app:

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]

What may be the problem here?

I am using Spring Boot Admin v1.5.7


Solution

  • The solution to this was to extend 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());
        }
        //...
    }
    

    and add it as a message converter at app configuration:

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

    because I was receiving an encrypted payload as plain/text content type.