使用 Jersey 从 Java 调用 POST API 发送 Text/CSV 文件
Sending Text/CSV file by an POST API call from Java using Jersey
我想通过发出 POST 请求将文件作为请求正文的一部分发送。文件的内容类型为 text/csv。我正在创建一个 MultiPart,但没有发送 text/csv 文件的选项。我尝试创建自定义内容类型 - text/csv 但它抛出异常。
下面是我的虚拟代码。
package com.practice;
import java.io.ByteArrayOutputStream;
import java.io.File;
import javax.ws.rs.client.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
public class CATestonlyForLocal1 {
public final static String TEXT_CSV = "text/csv";
public static final String CA_ENDPOINT_URL = "https://api.channeladvisor.com/v1/ProductUpload?";
public static final String CA_INVENTORY_TEMPLATE_CODE = "&templatecode=CAINV";
public static final String CA_PRICE_TEMPLATE_CODE = "CAPRICE";
public static final String CA_IMPORT_TYPE = "&importtype=AddUpdate";
public static final String CA_CONTENT_TYPE_VALUE = "text/csv";
public static final String CA_ACCESS_TOKEN = "wd52SrkWgoXeidEZ2nNIh_2gdveWIGWlKRYvUQQudYw-33727";
public static final String CA_FILE_FOLDER = "C:/D Drive/OMS Documents/Story Related Docs/Channel Advisor/InventoryUpload.txt";
public static final String CA_CONTENT_TYPE_KEY = "Content-Type";
public static void main(String[] args) {
try {
// getProductId();
uploadInventory();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void uploadInventory() {
final Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();
File fileEntity = new File(CA_FILE_FOLDER);
final FileDataBodyPart filePart = new FileDataBodyPart("text/csv", fileEntity);
final MediaType TEXT_CSV_TYPE = new MediaType("text", "csv");
filePart.setMediaType(TEXT_CSV_TYPE);
FormDataMultiPart multipart = new FormDataMultiPart();
multipart.bodyPart(filePart);
WebTarget target = client.target(CA_ENDPOINT_URL + "access_token=" + CA_ACCESS_TOKEN + CA_INVENTORY_TEMPLATE_CODE + CA_IMPORT_TYPE);
System.out.println("multipart.getMediaType(): " + multipart.getMediaType());
System.out.println(TEXT_CSV);
final Response response = target.request().post(Entity.entity(multipart,multipart.getMediaType()));
System.out.println(response.readEntity(String.class));
}
}
下面是我在使用自定义内容类型时遇到的异常情况
Apr 11, 2020 1:00:27 PM org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
SEVERE: MessageBodyWriter not found for media type=text/csv, type=class org.glassfish.jersey.media.multipart.FormDataMultiPart, genericType=class org.glassfish.jersey.media.multipart.FormDataMultiPart.
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/csv, 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:247)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1154)
at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:503)
at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:315)
at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:227)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:246)
at org.glassfish.jersey.client.JerseyInvocation.call(JerseyInvocation.java:667)
at org.glassfish.jersey.client.JerseyInvocation.call(JerseyInvocation.java:664)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:664)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:424)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:333)
at com.practice.CATestonlyForLocal.uploadInventory(CATestonlyForLocal.java:58)
at com.practice.CATestonlyForLocal.main(CATestonlyForLocal.java:32)
我已经阅读了很多文档,但没有得到太多。
请帮忙。提前致谢。
您必须使用受支持的媒体类型。
尝试
javax.ws.rs.core.MediaType.TEXT_PLAIN_TYPE
我想通过发出 POST 请求将文件作为请求正文的一部分发送。文件的内容类型为 text/csv。我正在创建一个 MultiPart,但没有发送 text/csv 文件的选项。我尝试创建自定义内容类型 - text/csv 但它抛出异常。 下面是我的虚拟代码。
package com.practice;
import java.io.ByteArrayOutputStream;
import java.io.File;
import javax.ws.rs.client.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
public class CATestonlyForLocal1 {
public final static String TEXT_CSV = "text/csv";
public static final String CA_ENDPOINT_URL = "https://api.channeladvisor.com/v1/ProductUpload?";
public static final String CA_INVENTORY_TEMPLATE_CODE = "&templatecode=CAINV";
public static final String CA_PRICE_TEMPLATE_CODE = "CAPRICE";
public static final String CA_IMPORT_TYPE = "&importtype=AddUpdate";
public static final String CA_CONTENT_TYPE_VALUE = "text/csv";
public static final String CA_ACCESS_TOKEN = "wd52SrkWgoXeidEZ2nNIh_2gdveWIGWlKRYvUQQudYw-33727";
public static final String CA_FILE_FOLDER = "C:/D Drive/OMS Documents/Story Related Docs/Channel Advisor/InventoryUpload.txt";
public static final String CA_CONTENT_TYPE_KEY = "Content-Type";
public static void main(String[] args) {
try {
// getProductId();
uploadInventory();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void uploadInventory() {
final Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();
File fileEntity = new File(CA_FILE_FOLDER);
final FileDataBodyPart filePart = new FileDataBodyPart("text/csv", fileEntity);
final MediaType TEXT_CSV_TYPE = new MediaType("text", "csv");
filePart.setMediaType(TEXT_CSV_TYPE);
FormDataMultiPart multipart = new FormDataMultiPart();
multipart.bodyPart(filePart);
WebTarget target = client.target(CA_ENDPOINT_URL + "access_token=" + CA_ACCESS_TOKEN + CA_INVENTORY_TEMPLATE_CODE + CA_IMPORT_TYPE);
System.out.println("multipart.getMediaType(): " + multipart.getMediaType());
System.out.println(TEXT_CSV);
final Response response = target.request().post(Entity.entity(multipart,multipart.getMediaType()));
System.out.println(response.readEntity(String.class));
}
}
下面是我在使用自定义内容类型时遇到的异常情况
Apr 11, 2020 1:00:27 PM org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
SEVERE: MessageBodyWriter not found for media type=text/csv, type=class org.glassfish.jersey.media.multipart.FormDataMultiPart, genericType=class org.glassfish.jersey.media.multipart.FormDataMultiPart.
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/csv, 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:247)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1154)
at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:503)
at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:315)
at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:227)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:246)
at org.glassfish.jersey.client.JerseyInvocation.call(JerseyInvocation.java:667)
at org.glassfish.jersey.client.JerseyInvocation.call(JerseyInvocation.java:664)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:664)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:424)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:333)
at com.practice.CATestonlyForLocal.uploadInventory(CATestonlyForLocal.java:58)
at com.practice.CATestonlyForLocal.main(CATestonlyForLocal.java:32)
我已经阅读了很多文档,但没有得到太多。
请帮忙。提前致谢。
您必须使用受支持的媒体类型。
尝试
javax.ws.rs.core.MediaType.TEXT_PLAIN_TYPE