我正在使用Jersey实现一个主要检索和提供JSON编码数据的RESTful API.但我有一些情况需要完成以下事项:
我有一个基于JQuery的单页Web客户端,它可以为这个Web服务创建AJAX调用.目前,它不进行表单提交,并使用GET和POST(使用JSON对象).我应该使用表单发送数据和附加的二进制文件,还是可以使用JSON plus二进制文件创建多部分请求?
我的应用程序的服务层当前在生成PDF文件时创建ByteArrayOutputStream.通过Jersey将此流输出到客户端的最佳方法是什么?我已经创建了一个MessageBodyWriter,但我不知道如何从Jersey资源中使用它.这是正确的方法吗?
我一直在查看泽西岛附带的样本,但还没有找到任何说明如何做这些事情的东西.如果重要的是,我正在使用泽西与杰克逊做没有XML步骤的对象 - > JSON,并没有真正使用JAX-RS.
我的要求是,我应该向客户发送一个10MB的zip文件,并提供宁静的服务.我在论坛中发现发送StreamingOutput对象的代码是更好的方法,但是如何StreamingOutput在以下代码中创建对象:
@Path("PDF-file.pdf/")
@GET
@Produces({"application/pdf"})
public StreamingOutput getPDF() throws Exception {
return new StreamingOutput() {
public void write(OutputStream output) throws IOException, WebApplicationException
{
try {
//------
} catch (Exception e) {
throw new WebApplicationException(e);
}
}
};
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Spring 4.0为RESTFUL Web服务创建POC.要求是从REST WEB-Service接收MultipartFile作为响应.
REST服务控制器
@RequestMapping(value="/getcontent/file", method=RequestMapping.post)
public MultipartFile getMultipartAsFileAsObject() {
File file = new File("src/test/resources/input.docx");
FileInputStream input = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile("file",file.getName(),
"application/docx", IOUtils.toByteArray(input));
return multipartFile
}
Run Code Online (Sandbox Code Playgroud)
我也使用第三方客户端和Apache Http客户端来调用此服务.请仔细看看输出.
使用第三方REST客户端即.邮差
输出看起来像Json -
{
"name" : "file",
"originalfilename" : "sample.docx",
"contentType" : "application/docx",
"content" : [
82,
101,
97,
100,
101,
32,
32,
.
.
.
.
.
]
}
Run Code Online (Sandbox Code Playgroud)
Apache HTTP Client示例代码
private static void executeClient() {
HttpClient client = new DefaultHttpClient();
HttpPost postReqeust = …Run Code Online (Sandbox Code Playgroud) 我想要一个像我这样的API的图像 localhost:8080:/getImage/app/path={imagePath}
当我点击这个API时,它会返回一个Image.
这可能吗?
实际上,我试过这个,但它给了我一个错误.这是我的代码,
@GET
@Path("/app")
public BufferedImage getFullImage(@Context UriInfo info) throws MalformedURLException, IOException {
String objectKey = info.getQueryParameters().getFirst("path");
return resizeImage(300, 300, objectKey);
}
public static BufferedImage resizeImage(int width, int height, String imagePath)
throws MalformedURLException, IOException {
BufferedImage bufferedImage = ImageIO.read(new URL(imagePath));
final Graphics2D graphics2D = bufferedImage.createGraphics();
graphics2D.setComposite(AlphaComposite.Src);
// below three lines are for RenderingHints for better image quality at cost of
// higher processing time
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.drawImage(bufferedImage, 0, 0, width, height, null);
graphics2D.dispose(); …Run Code Online (Sandbox Code Playgroud) 我试图使用JAX-RS从REST服务下载文件.这是我的代码,它通过发送GET请求来调用下载:
private Response invokeDownload(String authToken, String url) {
// Creates the HTTP client object and makes the HTTP request to the specified URL
Client client = ClientBuilder.newClient();
WebTarget target = client.target(url);
// Sets the header and makes a GET request
return target.request().header("X-Tableau-Auth", authToken).get();
}
Run Code Online (Sandbox Code Playgroud)
但是,我遇到将Response转换为实际File对象的问题.所以我做的是以下内容:
public File downloadWorkbook(String authToken, String siteId, String workbookId, String savePath)
throws IOException {
String url = Operation.DOWNLOAD_WORKBOOK.getUrl(siteId, workbookId);
Response response = invokeDownload(authToken, url);
String output = response.readEntity(String.class);
String filename;
// some code to retrieve the …Run Code Online (Sandbox Code Playgroud)