目前,我在Jersey中有一个方法,它从内容存储库中检索文件并将其作为响应返回.该文件可以是jpeg,gif,pdf,docx,html等(基本上任何东西).但是,目前我无法弄清楚如何控制文件名,因为每个文件都会自动下载名称(下载.[文件扩展名]即(download.jpg,download.docx,download.pdf).有没有办法我可以设置文件名吗?我已经把它放在一个字符串中,但我不知道如何设置响应,以便它显示文件名而不是默认为"下载".
@GET
@Path("/download/{id}")
public Response downloadContent(@PathParam("id") String id)
{
String serverUrl = "http://localhost:8080/alfresco/service/cmis";
String username = "admin";
String password = "admin";
Session session = getSession(serverUrl, username, password);
Document doc = (Document)session.getObject(session.createObjectId(id));
String filename = doc.getName();
ResponseBuilder rb = new ResponseBuilderImpl();
rb.type(doc.getContentStreamMimeType());
rb.entity(doc.getContentStream().getStream());
return rb.build();
}
Run Code Online (Sandbox Code Playgroud) 如何为简单的多表单帖子配置提供程序.任何建议/指针将不胜感激.
堆栈跟踪:
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=multipart/form-data, 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:227)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:149)
at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1139)
at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:433)
at org.glassfish.jersey.test.inmemory.internal.InMemoryConnector.apply(InMemoryConnector.java:214)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:217)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:655)
Run Code Online (Sandbox Code Playgroud)
pom.xml依赖项:
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-inmemory</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
代码:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.server.ResourceConfig; …Run Code Online (Sandbox Code Playgroud) 我找不到任何有用的例子,如何实现以下内容:我希望我的Swagger-UI中的API方法按方法(GET-POST-PUT-DELETE)或/按字母顺序排序.
到目前为止,所有方法都以随机顺序显示,甚至不按给定源代码的顺序显示.
我使用Jax-RS + Jersey 1.
使用@ApiOperation的position属性进行排序对我来说不是一个选项,因为有太多的方法而且API仍在扩展,所以如果有一个新的,我需要更新所有.
任何提示?
我正在尝试构建一个REST Web服务(服务器端),它允许合作伙伴系统以JSON格式连接/ POST订单信息.我应该使用JAX RS(例如来自JBOSS RESTEasy)还是使用Spring MVC来构建这样的服务?就构建REST服务而言,它们似乎都有足够的能力完成同样的事情.
谢谢!
CXF JAXRS通过处理If-Match,If-Modified-Since和ETags标头提供对许多高级HTTP功能的支持.JAXRS请求上下文对象可用于检查前提条件.还支持Vary,CacheControl,Cookies和Set-Cookies.
我真的很想使用(或至少探索)这些功能.然而,虽然"提供支持"的声音非常有趣,但它在实现这些功能方面并不是特别有用.有关如何使用If-Modified-Since,CacheControl或ETags的任何帮助或指示?
澄清:这个问题是关于GZIPping基于JAX-WS的REST服务,但我决定改变主题以便更容易找到
我正在通过JAX-WS实现REST服务Provider <Source>,并使用标准发布它Endpoint(原因是我想避免使用servlet容器或应用程序服务器).
有没有办法使服务器gzip响应内容,如果Accept-Encoding: gzip存在?
nicore实际提供的示例工作,它允许您在没有servlet容器的嵌入式轻量级服务器之上制作JAX-RS样式的服务器,但是几乎没有时间可以考虑.
如果您希望自己管理类(并在启动期间节省时间),则可以使用以下命令:
JAX-RS你好世界级:
@Path("/helloworld")
public class RestServer {
@GET
@Produces("text/html")
public String getMessage(){
System.out.println("sayHello()");
return "Hello, world!";
}
}
Run Code Online (Sandbox Code Playgroud)
主要方法:
对于简单服务器:
public static void main(String[] args) throws Exception{
DefaultResourceConfig resourceConfig = new DefaultResourceConfig(RestServer.class);
// The following line is to enable GZIP when client accepts it
resourceConfig.getContainerResponseFilters().add(new GZIPContentEncodingFilter());
Closeable server = SimpleServerFactory.create("http://0.0.0.0:5555", resourceConfig);
try {
System.out.println("Press any key to stop the service...");
System.in.read();
} …Run Code Online (Sandbox Code Playgroud) 是否有任何方法可以自动生成一个带有HttpUrlConnection的jax-rs客户端或者第三方解决方案,如jersey,apache,restlet等.拥有一个框架/应用程序不是一个优势吗?
在详尽搜索了Web和Stackoverflow之后,我仍然在试图弄清楚如何集成Jersey和Jetty提供的RESTlet样式接口.
我的Jetty服务器已启动并运行,因此Jersey似乎也很容易使用,有没有人知道如何将两者结合在一起?任何具体的链接都会有所帮助 - 我对servlet编程也有点新意.
我有一个带JAXB注释的员工类:
@XmlRootElement(name = "employee")
public class Employee {
private Integer id;
private String name;
...
@XmlElement(name = "id")
public int getId() {
return this.id;
}
... // setters and getters for name, equals, hashCode, toString
}
Run Code Online (Sandbox Code Playgroud)
还有一个JAX-RS资源对象(我使用的是Jersey 1.12)
@GET
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("/")
public List<Employee> findEmployees(
@QueryParam("name") String name,
@QueryParam("page") String pageNumber,
@QueryParam("pageSize") String pageSize) {
...
List<Employee> employees = employeeService.findEmployees(...);
return employees;
}
Run Code Online (Sandbox Code Playgroud)
此端点工作正常.我明白了
<employees>
<employee>
<id>2</id>
<name>Ana</name>
</employee>
</employees>
Run Code Online (Sandbox Code Playgroud)
但是,如果我更改方法以返回Response对象,并将员工列表放在响应正文中,如下所示:
@GET
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用简单的Jersey + JSON示例,但我得到以下错误
message body writer for Java class com.test.jsonexample and MIME media type application/json was not found
我把以下jar文件用于获得适当的结果
asm-3.1.jar
jackson-core-asl-1.9.9.jar
jackson-jaxrs-1.9.9.jar
jackson-mapper-asl-1.9.9.jar
jackson-xc-1.9.9.jar
jersey-client-1.9.jar
jersey-core-1.9.1.jar
jersey-json-1.9.jar
jersey-server-1.9.1.jar
jettison-1.3.2.jar
jsr311-api-1.1.1.jar
Run Code Online (Sandbox Code Playgroud)
为什么我会收到此类错误?错误日志在这里:
SEVERE: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class com.test.Jsonexample, and Java type class com.test.Jsonexample, and MIME media type application/json was not found
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1437)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at …Run Code Online (Sandbox Code Playgroud) jax-rs ×10
java ×6
jersey ×4
rest ×4
cxf ×1
http-caching ×1
jaxb ×1
jetty ×1
json ×1
resteasy ×1
servlets ×1
spring-mvc ×1
swagger ×1
swagger-ui ×1
wadl ×1