【问题标题】:Spring boot and Jersey javax.ws.rs.ProcessingException: No available MessageBodyWriterSpring Boot 和 Jersey javax.ws.rs.ProcessingException:没有可用的 MessageBodyWriter
【发布时间】:2017-10-20 23:03:48
【问题描述】:

尝试使用 Spring Boot 和 Jersey 调用 REST Client。 但是遇到了问题

javax.ws.rs.ProcessingException: No available MessageBodyWriter for class "class org.glassfish.jersey.media.multipart.file.FileDataBodyPart" and media type "multipart/mixed"

尝试使用 Jersey 版本 2.6 确保我已经注册了 MultiPartFeature

Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();

需要在 HTTP 请求中发送 multipart/mixed 作为内容类型。 任何其他想法或帮助将不胜感激。

MultiPart 主体的示例代码

MultiPart multiPartEntity = new MultiPart()
    .bodyPart(new BodyPart(new FormDataBodyPart("XXXX", payload), 
javax.ws.rs.core.MediaType.TEXT_XML_TYPE))
    .bodyPart(new BodyPart(new FileDataBodyPart("YYYYY",file), j 
javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM_TYPE));

【问题讨论】:

    标签: spring-boot jersey


    【解决方案1】:

    类“类 FileDataBodyPart”没有可用的 MessageBodyWriter

    您不能使用FileDataBodyPart 作为实体主体。它必须是一个MultiPart,您可以添加一个FileDataBodyPart。例如

    FileDataBodyPart filePart = new FileDataBodyPart("test", new File("test.txt"));
    MultiPart multiPart = new FormDataMultiPart()
            .bodyPart(filePart);
    
    Response response = ClientBuilder.newClient()
            .register(MultiPartFeature.class)
            .target(url)
            .request()
            .post(Entity.entity(multiPart, "multipart/mixed"));
    

    更新(完整测试代码)

    不确定你做错了什么,但这是我测试过的完整代码(使用Jersey Test Framework)。根据 OP 的评论,我将下面的代码更改为使用 MultiPart 而不是 FormDataMultiPart,但无论哪种方式,请求仍然以多部分/混合内容类型发送,因为这是我使用 @987654328 明确设置的@。

    import org.glassfish.jersey.filter.LoggingFilter;
    import org.glassfish.jersey.media.multipart.*;
    import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
    import org.glassfish.jersey.server.ResourceConfig;
    import org.glassfish.jersey.test.JerseyTest;
    import org.junit.Test;
    
    import javax.ws.rs.Consumes;
    import javax.ws.rs.POST;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.client.ClientBuilder;
    import javax.ws.rs.client.Entity;
    import javax.ws.rs.core.Response;
    import javax.ws.rs.ext.ExceptionMapper;
    import java.io.*;
    import java.util.logging.Logger;
    
    import static org.junit.Assert.assertEquals;
    
    public class MutliPartMixedTest extends JerseyTest {
    
        @Path("test")
        public static class TestResource {
    
            @POST
            @Consumes("multipart/mixed")
            @Produces("text/plain")
            public String post(MultiPart multiPart) throws Exception {
                BodyPart part = multiPart.getBodyParts().get(0);
                return part.getEntityAs(String.class);
            }
        }
    
        @Override
        public ResourceConfig configure() {
            return new ResourceConfig()
                    .register(TestResource.class)
                    .register(MultiPartFeature.class)
                    .register(new LoggingFilter(Logger.getAnonymousLogger(), true))
                    .register(new ExceptionMapper<Throwable>() {
                        @Override
                        public Response toResponse(Throwable t) {
                            t.printStackTrace();
                            return Response
                                    .serverError()
                                    .entity(t.getMessage())
                                    .build();
                        }
                    });
        }
    
        @Test
        public void doIt() throws Exception {
            File file = new File("test.txt");
            try (Writer writer = new BufferedWriter(new FileWriter(file))) {
                writer.write("Hello World!");
            }
            FileDataBodyPart filePart = new FileDataBodyPart("data", file);
            MultiPart multiPart = new MultiPart()
                    .bodyPart(filePart);
    
            try {
                Response response = ClientBuilder.newClient()
                        .register(MultiPartFeature.class)
                        .target("http://localhost:9998/test")
                        .request()
                        .accept("text/plain")
                        .post(Entity.entity(multiPart, "multipart/mixed"));
    
              assertEquals("Hello World!", response.readEntity(String.class));
            } finally {
                file.delete();
            }
        }
    }
    

    【讨论】:

    • 我确实以同样的方式构建了它,而是使用了 MultiPart multiPartEntity = new MultiPart(),因为内容类型应该是多部分/混合的。我在上面添加了示例代码。
    • 不确定您是否说它仍然无法正常工作,但我发布了一个完整的工作测试用例。
    • 感谢您的示例,这有帮助.. 我们最终得到了一个没有球衣的不同解决方案。
    • 顺便说一句..你有弹簧靴的例子吗?
    • 尽管注册了 MultiPartFeature 我一直收到同样的错误。经过一番艰苦的斗争,我决定不使用我们的项目特定构建器,而是使用全新的构建器,它开始工作了。所以我的建议是——当遇到麻烦时,尝试新的简单的客户端创建。我的猜测是,在我们的 API 中,MPF 的注册不知何故被另一个更早注册的组件覆盖。
    猜你喜欢
    • 2021-07-30
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 2021-10-17
    • 2015-01-12
    • 2018-11-08
    相关资源
    最近更新 更多