- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我目前正在实现我的第一个基于 JAX-RS 的 REST 服务,在我对 JAX-RS 的研究过程中,我发现了两个版本如何实现应用程序的主要“入口点”。一种选择是实现一个在其主要方法中启动服务器的类:
public class Spozz {
public static void main(String[] args) throws Exception {
//String webappDirLocation = "src/main/webapp/";
int port = Integer.parseInt(System.getProperty("port", "8087"));
Server server = new Server(port);
ProtectionDomain domain = Spozz.class
.getProtectionDomain();
URL location = domain.getCodeSource().getLocation();
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setDescriptor(location.toExternalForm() + "/WEB-INF/web.xml");
webapp.setResourceBase(location.toExternalForm());
webapp.setServer(server);
webapp.setWar(location.toExternalForm());
webapp.setParentLoaderPriority(true);
// (Optional) Set the directory the war will extract to.
// If not set, java.io.tmpdir will be used, which can cause problems
// if the temp directory gets cleaned periodically.
// Your build scripts should remove this directory between deployments
webapp.setTempDirectory(new File(location.toExternalForm()));
server.setHandler(webapp);
server.start();
server.join();
}
}
另一个扩展 javax.ws.rs.core.Application
类。
@ApplicationPath("/services")
public class Spozz extends Application {
private Set<Object> singletons = new HashSet<Object>();
private Set<Class<?>> empty = new HashSet<Class<?>>();
public Spozz() {
singletons.add(new UserResource());
}
@Override
public Set<Class<?>> getClasses() {
return empty;
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
然而,我在许多不同的示例中都看到了这两个版本,但从未解释过为什么选择这个版本以及有什么好处。我个人会坚持第二个,但我真的没有理由这样做。所以我的问题是:
最佳答案
What are the differences of this options?
选项 #1 实际上只是作为一个快速启动。您希望在生产环境中调整 Web 容器的许多属性,例如线程池/线程数/连接器等。我不确定您的代码与哪个嵌入式容器相关,但 Web 容器配置最好留给配置文件而不是生产中的代码。
Which one is preferable when using JAX-RS 2.0?
如果您正在制作原型(prototype),请使用#1。用于生产用途 #2。对于 Jersey 应用程序的入口点,扩展 Application
类是可行的方法。所以这不是真正的选择问题。
Is it important if I would like to add servlets to the service?
我不确定那是什么意思。您希望稍后添加 servlet 吗?无论您使用的是嵌入式容器还是独立容器,都没有关系。 Servlet 就是 servlet。它们将在容器中处理请求。
关于java - JAX-RS 网络服务的主要应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22503042/
来自 JSR-339: For simplicity, JAX-RS implementations are NOT REQUIRED to support processing groups oth
JAX-RS 提供了 StreamingOutput 接口(interface),我们可以实现它来对我们的响应主体进行原始流处理。 public interface StreamingOutput {
有没有办法获得java.lang.reflect.Method为给定的 @Path 调用的方法(用 HttpServletRequest 注释) ? 这是我的用例:我在 Java EE 中 Filte
我接到了一家公司的任务,该任务向我发送了一台已完成所有设置的虚拟机。任务是我必须创建一个 API 来从数据库中检索人员详细信息并显示它。 问题是,当我运行应用程序时,服务器返回一个包含 hello w
我有一个 POST 方法调用,它接受很少的表单参数。我希望 JAX-RS 能够处理不存在特定表单参数的情况。 示例: @POST @Produces (MediaType.APPLICATION_JS
我有 JAX-RS 网络应用程序,我想记录从获取请求到响应的时间量。在带有 servlet 过滤器的 Spring Boot 中很容易。但是我的应用程序中的过滤器无法正常工作: @Provider p
使用以下网址。 http://doma.in/context/resource/some/.../undefined 我想获取 ../resource 之后的路径名,即 /some/.../undef
Closed. This question is off-topic。它当前不接受答案。
我花了几个小时在嵌入式 Jetty 9.1.0.v20131115 和 RESTEasy 3.0.5.Final 中安装自定义登录服务。我的登录服务将在数据库中查找用户并为他们分配角色。它看起来像这样
RESOURCE_STALLS.RS的说明Intel Broadwell 的硬件性能事件如下: This event counts stall cycles caused by absence of
我想从 java 服务器 (Restful Jax-rs) 发送图像。我的客户是安卓。 @GET public Response getUserImage() { byte[] image =new
(特别是 RESTeasy) 具有如下方法签名会很好(对于单个文件): public void upload(@FormParam("name") ..., @FormParam("file") fi
RESTEasy (JAX-RS) 允许通过子资源进行动态调度。例如: POST /customers/create {"name":"Smith","country":"jp"} 我们可以有一个根资
假设有一个 ResultSet rs 和 n 对象。 这段代码: while(rs.next()) { // do something on rs } 在算法上等于此代码(即两者给出相同的结果)
我正在使用 Visual Studio 2005 和 mysql 4.0,我有一个表,其中包含一个字段(十进制 16,4),用于存储发票金额和另一个表,其中包含对该发票的付款(这些字段也是十进制的)
我正在使用 JAX-RS、Microprofile 和 Payara 5 构建 REST 服务。我的方法返回类型为 Response 的对象。响应本身包含一个 MyClass 列表。实现看起来像这样:
我已经使用 Mock MVC 成功地为 Spring MVC REST 服务构建了无需部署的集成测试框架。我正在努力对 JAX-RS 服务做同样的事情。 REST Assured 看起来很有希望,但它
我有一个使用JAX-RS 2.0的Jersey实现的Java应用程序,并且我想在客户端启用gzip压缩。服务器已启用它,并且通过在Chrome中的开发人员工具中的“大小/内容”中查找客户端正在使用的特
我正在尝试与 Tomcat7 一起使用 RESTful Web 服务 (JAX-RS)。我尝试了 3 种不同的实现(Jersey、RESTeasy 和 ReSTLet),但都没有成功。这应该很容易,但
swagger-api有什么区别和 JAX-RS ? 是swagger-api仅用于文档? (例如 @ApiOperation ) 最佳答案 根据 API 文档,JAX-RS是用于 RESTful W
我是一名优秀的程序员,十分优秀!