No serializer found for class java.io.ByteArrayInputStream

北城以北 提交于 2021-02-08 15:13:51

问题


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.


回答1:


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



回答2:


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);
    }
}



回答3:


Another way to solve this issue is to ignore the property which has return type of ByteArrayInputStream, for example you could decorate this class:

class Frodo {
    private bytes[] value;
    public Frodo(bytes[] value) {
        this.value = value;
    }

    public ByteArrayInputStream getFoo() throws IOException {
        return new ByteArrayInputStream(value());
    }

    public void setFoo(ByteArrayInputStream streamData) {
        // set value from streamData
    }
}

like this:

@JsonIgnoreProperties(value = { "foo" })
class Frodo {
    private bytes[] value;
    public Frodo(bytes[] value) {
        this.value = value;
    }

    public ByteArrayInputStream getFoo() throws IOException {
        return new ByteArrayInputStream(value());
    }

    public void setFoo(ByteArrayInputStream streamData) {
        // set value from streamData
    }
}

you can also ignore multiple properties: @JsonIgnoreProperties(value = { "foo", "anotherValue" })



来源:https://stackoverflow.com/questions/46212246/no-serializer-found-for-class-java-io-bytearrayinputstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!