Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No serializer found for class java.io.ByteArrayInputStream

I am getting the below error message while getting the user entity from the openfire rest api. ( I am wrapping the my Api Endpoints with openfire Restapi Endpoints.)

"error": "Internal Server Error", "exception": "org.springframework.http.converter.HttpMessageNotWritableException", "message": "Could not write JSON: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.mashape.unirest.http.HttpResponse[\"rawBody\"])", "path": "/usersInfo/user2"

The code is the following.

String  host ="http://abdul01anpi01:9090" ;
String userEndPoint = "/plugins/restapi/v1/users" ;
String apiURL = host+userEndPoint ;
HttpResponse<JsonNode> response =null;

response = Unirest.get(apiURL +"/{username}").header("accept", "application/json").header("Content-Type", "application/json").routeParam("username",String.valueOf(username)).asJson();

The expected output from the response is the following.

{
    "username": "user2",
    "name": "user2",
    "properties": null
}

Kindly advise, any help is appreciated.

like image 487
abdul Avatar asked Sep 14 '17 06:09

abdul


2 Answers

The poster found a solution and posted it in a comment. Since it's been a few years, I figured it might be worth copying as an actual answer:

@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(mapper);
    return converter;
}

Let me add the description for the flag that is being set to false :

/**
 * Feature that determines what happens when no accessors are
 * found for a type (and there are no annotations to indicate
 * it is meant to be serialized). If enabled (default), an
 * exception is thrown to indicate these as non-serializable
 * types; if disabled, they are serialized as empty Objects,
 * i.e. without any properties.
 *<p>
 * Note that empty types that this feature has only effect on
 * those "empty" beans that do not have any recognized annotations
 * (like <code>@JsonSerialize</code>): ones that do have annotations
 * do not result in an exception being thrown.
 *<p>
 * Feature is enabled by default.
 */
FAIL_ON_EMPTY_BEANS
like image 83
payne Avatar answered Nov 18 '22 19:11

payne


it works, add a ResourceHttpMessageConverter!

@Configuration
public class EirExceptionConfig extends WebMvcConfigurerAdapter {
    @Autowired
    ObjectMapper objectMapper;

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(EirException.class, new EirExceptionJackson2Serializer());
        objectMapper.registerModule(simpleModule);
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(objectMapper);
        converters.add(new ResourceHttpMessageConverter());
        converters.add(converter);
    }
}
like image 1
疯子Max Avatar answered Nov 18 '22 18:11

疯子Max