解决jersey客户端org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=multipart/form-data, type=class org.glassfish.jersey.media.multipart.FormDataMultiPart, genericType=class org.glassfish.jersey.media.multipart.FormDataMultiPart.

开发rest客户端时,遇到org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException异常,详细信息如下:

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=multipart/form-data, type=class org.glassfish.jersey.media.multipart.FormDataMultiPart, genericType=class org.glassfish.jersey.media.multipart.FormDataMultiPart.
	at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:248) ~[jersey-common-2.26.jar:na]
	at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:163) ~[jersey-common-2.26.jar:na]
	at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1135) ~[jersey-common-2.26.jar:na]
	at org.glassfish.jersey.client.ClientRequest.doWriteEntity(ClientRequest.java:516) ~[jersey-client-2.26.jar:na]
	at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:498) ~[jersey-client-2.26.jar:na]
	at org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:384) ~[jersey-client-2.26.jar:na]
	at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:282) ~[jersey-client-2.26.jar:na]
	at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:278) ~[jersey-client-2.26.jar:na]
	at org.glassfish.jersey.client.JerseyInvocation.lambda$invoke$0(JerseyInvocation.java:753) ~[jersey-client-2.26.jar:na]
	at org.glassfish.jersey.internal.Errors.process(Errors.java:316) ~[jersey-common-2.26.jar:na]
	at org.glassfish.jersey.internal.Errors.process(Errors.java:298) ~[jersey-common-2.26.jar:na]
	at org.glassfish.jersey.internal.Errors.process(Errors.java:229) ~[jersey-common-2.26.jar:na]
	at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:414) ~[jersey-common-2.26.jar:na]
	at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:752) ~[jersey-client-2.26.jar:na]
	at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:445) ~[jersey-client-2.26.jar:na]
	at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:351) ~[jersey-client-2.26.jar:na]
......
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) [tomcat-embed-core-8.5.15.jar:8.5.15]
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.15.jar:8.5.15]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_91]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_91]
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.15.jar:8.5.15]
	at java.lang.Thread.run(Thread.java:745) [na:1.8.0_91]


经过分析是因为缺少FormDataMultiPart写入流的MessageBodyWriter。解决方法如下:

创建Client实例时,指定MessageBodyWriter,即MultiPartFeature.class

    	Configuration clientConfig = new ClientConfig(MultiPartFeature.class);
        Client client = ClientBuilder.newClient(clientConfig);

发表评论

电子邮件地址不会被公开。 必填项已用*标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.