找不到类 java.io.ByteArrayInputStream 的序列化程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46212246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:06:39  来源:igfitidea点击:

No serializer found for class java.io.ByteArrayInputStream

javaspringunirest

提问by abdul

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.)

从 openfire rest api 获取用户实体时,我收到以下错误消息。(我正在用 openfire Restapi 端点包装我的 Api 端点。)

"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.Hymanson.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"

“错误”:“内部服务器错误”,“异常”:“org.springframework.http.converter.HttpMessageNotWritableException”,“消息”:“无法编写 JSON:找不到类 java.io.ByteArrayInputStream 的序列化程序,也未发现任何属性创建 BeanSerializer(为了避免异常,禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS);嵌套异常是 com.fasterxml.Hymanson.databind.JsonMappingException: No serializer found for class java.io.ByteArrayInputStream 并且没有发现创建 BeanSerializer 的属性(为了避免异常,禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过参考链: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.

请建议,任何帮助表示赞赏。

回答by u2705707

it works, add a ResourceHttpMessageConverter!

它有效,添加一个 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 EirExceptionHymanson2Serializer());
        objectMapper.registerModule(simpleModule);
        MappingHymanson2HttpMessageConverter converter = new MappingHymanson2HttpMessageConverter(objectMapper);
        converters.add(new ResourceHttpMessageConverter());
        converters.add(converter);
    }
}

回答by payne

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 MappingHymanson2HttpMessageConverter mappingHymanson2HttpMessageConverter() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    MappingHymanson2HttpMessageConverter converter = new MappingHymanson2HttpMessageConverter(
            mapper);
    return converter;
}

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

让我为正在设置的标志添加描述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

回答by Benas

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

解决此问题的另一种方法是忽略返回类型为 的属性ByteArrayInputStream,例如您可以装饰此类:

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

您还可以忽略多个属性: @JsonIgnoreProperties(value = { "foo", "anotherValue" })