[英]cxf restful return image
I'm new using CXF and Spring to make RESTful webservices. 我是使用CXF和Spring制作RESTful Web服务的新手。 The project is under maven control.
该项目处于Maven控制之下。
I have a android app that would like to connect to the server retrieving book information and cover picture via RESTful webservices. 我有一个Android应用程序,该应用程序想连接到服务器,以通过RESTful网络服务检索书籍信息和封面图片。 Is it possible (and how) RESTful service return an image?
RESTful服务是否可能(以及如何)返回图像?
Project Explorer 项目浏览器
Project
--Java Resource
----src/main/java
------library.service
--------IBook.java
--------Book.java
----images
------Cover1.png
------Cover2.png
--JavaResources
--Deployed Resources
----webapp
------WEB-INF
----web-resources
IBook.java interface iBook.java接口
@GET
@Path("/book/cover/{name}")
@Produces("image/png")
public Image getImage(@PathParam("name") String name);
Book.java impl Book.java展示
public Image getImage(String name){
//How should I get cover png from 'images' folder and return it??
}
Thanks 谢谢
Silvester Pang 西尔维斯特·庞
You can return your selected image via Response.ok(params...).build() 您可以通过Response.ok(params ...)。build()返回您选择的图像
Change your IBook.java to 将您的IBook.java更改为
@GET
@Path("/book/cover/{name}")
@Produces("image/*")
public Response getImage(@PathParam("name") String name);
And your Implementing Interface to this. 还有您的实现接口。
public Response getImage(String name){
File file = new File(fileUrl);
String mediaType = SomeContentTypeMapHere(file)
return Response.ok(file,mediaType).build()
}
EDIT 编辑
We've used Response.ok(Object type, String mediaType).build()
to Build and return a Response when our method handler is called. 我们已经使用
Response.ok(Object type, String mediaType).build()
来构建并在调用我们的方法处理程序时返回Response。 The First parameter is an Object. 第一个参数是一个对象。 think it of as the Thing we're going to send when somebody/someones sends a request on the given url while mediaType is basically the Content type(eg, "image/png, image/jpg and etc) of what we're going to send.
认为这是当某人/某人在给定的url上发送请求时我们将要发送的东西,而mediaType基本上是我们要去的内容类型(例如,“ image / png,image / jpg等”)发送。
Remember to match your @Produces("image/*")
to the content type you are passing on the ok(Object entity, String mediaType
method. 请记住将
@Produces("image/*")
与您在ok(Object entity, String mediaType
方法@Produces("image/*")
传递的内容类型进行ok(Object entity, String mediaType
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.