Phi*_*lux 4 java rest web-services jax-ws
我目前正在实现我的第一个基于JAX-RS的REST服务,在我对JAX-RS的研究中,我发现了两个版本如何实现应用程序的主要"入口点".一种选择是实现一个在其main方法中启动服务器的类:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
另一个是扩展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;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我在许多不同的例子中都看过这两个版本,但是从未解释为什么选择这个版本以及它带来了什么好处.我个人会坚持第二个,但我没有理由这样做.所以我的问题是:
这个选项有什么不同?
选项#1实际上只是一个快速入门.您希望在生产环境中调整Web容器的许多属性,例如线程池/线程数/连接器/等.我不确定您的代码与哪个嵌入式容器相关,但Web容器配置最好留下配置文件,而不是生产中的代码.
使用JAX-RS 2.0时哪一个更好?
如果您正在进行原型设计,请使用#1.用于生产用途#2.对于进入泽西应用程序的入口点,扩展Application课程是最佳选择.所以这不是一个真正的选择问题.
如果我想将servlet添加到服务中,这很重要吗?
我不确定这意味着什么.你想在以后添加servlet吗?如果您使用的是嵌入式容器或独立容器,则无关紧要.Servlet是servlet.当他们在一个容器中时,他们会提出请求.
| 归档时间: |
|
| 查看次数: |
1210 次 |
| 最近记录: |