我在Glassfish服务器(GF4)上实现了一个REST服务,还有一个小客户端程序来测试它。但服务器响应“状态415 - 不支持的媒体类型”错误。请查看我的代码并给我一些提示。
服务器代码:
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.MessagingException;
import javax.ws.rs.GET;
@Path("generic")
public class GenericResource {
@Context
private UriInfo context;
public GenericResource() {
System.out.println("Mediatype: "+MediaType.MULTIPART_FORM_DATA);
}
@Path("post")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void postXml(javax.mail.internet.MimeMultipart multiPart) {
try {
System.out.println(multiPart.getCount());
} catch (MessagingException ex) {
Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
这是我的客户代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.HttpClientBuilder;
public class URLTester {
public static void main(String[] args) throws Exception {
URLTester http = new URLTester();
http.sendPost();
}
// HTTP POST request
private void sendPost() throws Exception {
String filename = "C:\\development\\java\\URLTester\\test.obx";
String xml = readXML(filename);
String url = "http://localhost:8080/RESTgarbio2oex/webresources/generic/post";
HttpClient httpclient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
// MultiPart POST
FileBody fb = new FileBody(new File(filename));
post.setHeader("Content-type", "multipart/form-data");
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.addPart("file", fb);
multipartEntity.addTextBody("data", "Here is the data.");
post.setEntity(multipartEntity.build());
HttpResponse response = httpclient.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String inputLine;
while ((inputLine = rd.readLine()) != null) {
System.out.println(inputLine.toString());
}
}
private String readXML(String xmlsource) {
String xml = "";
try {
File in = new File(xmlsource);
xml = FileUtils.readFileToString(in);
} catch (IOException ex) {
Logger.getLogger(URLTester.class.getName()).log(Level.SEVERE, null, ex);
}
return xml;
}
}
如您所见,我将内容类型设置为"multipart/form-data"
,并使用MultipartEntityBuilder创建要发送的数据。我还使用Apache HTTPClientBuilder类将数据发布到服务器。
在服务器端,根本没有调用postXml
方法,即使我的调试控制台说调用了构造函数并将Mediatype: multipart/form-data
作为结果。
如果您需要更多信息,请告诉我。我搜索了很多网站,但没有遇到我的问题的解决方案。提前谢谢。