我是网络服务的新手。由于要求,我必须通过REST Web服务将文件[最有可能以txt格式]发送到服务器。
我收到如下的例外情况。
找不到媒体类型= application / json的MessageBodyWriter,类型= class gvjava.org.json.JSONObject,genericType = class gvjava.org.json.JSONObject。
这是我的网络服务方法。
@Path("{c}")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String convert(@PathParam("c") JSONObject object) throws JSONException {
String result = "";
return "<ctofservice>" + "<ctofoutput>" + result + "</ctofoutput>" + "</ctofservice>";
}
现在客户端代码如下
JSONObject data_file = new JSONObject();
data_file.put("file_name", uploadFile.getName());
data_file.put("description", "Something about my file....");
data_file.put("file", uploadFile);
Client client = ClientBuilder.newClient();
webTarget = client.target(uploadURL).path("ctofservice").path("convert");
Response value = webTarget.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(data_file,MediaType.APPLICATION_JSON_TYPE),
Response.class);
请帮我解决这个问题。 提前谢谢。
-------------------------------------------- ----------------------------
正如下面的答案中的 peeskillet 所示,我尝试通过multipart发送文件。我仍然面临没有发现八位字节流的例外情况。
以下是我的休息api
@Path("{c}")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response convert(@FormDataParam("file") FormDataContentDisposition file) {
String result = "";
Some operation with attached parameter ...
return Response.status(200).entity(result).build();
}
这是我的测试客户端
FormDataMultiPart multiPart = new FormDataMultiPart();
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file",
uploadFile,MediaType.APPLICATION_OCTET_STREAM_TYPE);
multiPart.bodyPart(fileDataBodyPart);
Client client = Client.create();
WebResource webResource = client
.resource(uploadURL).path("ctofservice");
ClientResponse response = webResource.accept("application/json")
.post(ClientResponse.class,multiPart);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
我正在获得以下例外
我无法理解为什么我需要将数据发送为 MediaType.APPLICATION_OCTET_STREAM_TYPE ?因为我之前使用过multipart作为媒体类型...
感谢您的帮助..
答案 0 :(得分:0)
无需配置任何其他内容,解决此问题的最简单方法是使用字符串而不是实际toString()
(即只传递.post(Entity.json(data_file.toString()))
)
JSONObject
使用JSONObject
的问题在于没有提供商知道如何处理转换。您将在服务器端遇到同样的问题,因为没有提供商可以处理到@POST
public Response post(String json) {
JSONObject jsonObject = new JSONObject(json);
}
的转换。所以你需要做的就是
JSONObject
如果您真的希望能够在不需要使用字符串的情况下使用"<ctofservice>" + "<ctofoutput>" + result + "</ctofoutput>" + "</ctofservice>"
,那么您应该查看https://cldex.net/visual/cinema_telaviv.geojson。
顺便说一句,这不是有效的JSON(它的XML)
columnText.getCanvas().setColorFill(Color.decode("#FFA500"));
columnText.getCanvas().rectangle(llx, ury-(leading) ,columnText.getWidth(phrase) , sizeOftext);
columnText.getCanvas().fill();
columnText.go();
但是你说端点返回JSON