den*_*mus 13 java servlets glassfish jax-rs jersey
我正在研究在Glassfish上运行的应用程序.我应该通过使用jax-rs和jersey将servlet转换为适当的restful内容.
我一直试图找到init()方法的解决方法,但直到现在我都失败了.
这是原始部分,使用servlet:
import javax.servlet.*
public void init(ServletConfig config) throws ServletException {
super.init(config);
if (!isRunning() == true)) {
/* Do some stuff here*/
}
logger.info("Deamon has started");
}
Run Code Online (Sandbox Code Playgroud)
而这个我试图使用jax-rs
import javax.ws.rs.*
import javax.servlet.*
public void init(@Context ServletConfig config) throws ServletException {
//uper.init(config);
if (!isRunning() == true)) {
/* Do some stuff here*/
}
logger.info("Deamon has started");
}
Run Code Online (Sandbox Code Playgroud)
我检查过邮件列表并用Google搜索,但无法找到适用于此案例的方法.
任何想法如何使用servlet为init方法实现相同的行为?
den*_*mus 10
最后,谷歌搜索了一点之后,我找到了一个合适的解决方案.
基本上,我有扩展
public class ContextListener implements ServletContextListener类并实现了public void contextInitialized(ServletContextEvent sce)在加载应用程序时调用的抽象方法.我已经将逻辑从servlet移到这里进行初始化和其他配置设置,然后它很顺利.
使用@PostConstruct ; 来自Web应用程序的示例:
@Context
private ServletContext context;
@PostConstruct
public void init() {
// init instance
}
Run Code Online (Sandbox Code Playgroud)
以下是我在Jersey 2.6/JAX-RS中实现init方法的方法,以防它对任何人有帮助.这是使用@PostConstruct的建议.
下面的代码启动Web应用程序,扫描包中的所有资源并使用3初始化静态测试计数器:
package com.myBiz.myWebApp;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.net.URI;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ws.rs.core.Application;
public class WebApplication extends Application {
// Base URI the HTTP server will listen to
public static final String BASE_URI = "http://localhost:8080/";
public static int myCounter = 0;
/**
* Starts a server, initializes and keeps the server alive
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
final HttpServer server = startServer();
initialize();
System.out.println("Jersey app started\nHit enter to stop it...");
System.in.read();
server.stop(1);
System.out.println("Server stopped successfully.");
}
/**
* Default constructor
*/
public WebApplication() {
super();
}
/**
* Initialize the web application
*/
@PostConstruct
public static void initialize() {
myCounter = myCounter + 3;
}
/**
* Define the set of "Resource" classes for the javax.ws.rs.core.Application
*/
@Override
public Set<Class<?>> getClasses() {
return getResources().getClasses();
}
/**
* Scans the project for REST resources using Jersey
* @return the resource configuration information
*/
public static ResourceConfig getResources() {
// create a ResourceConfig that scans for all JAX-RS resources and providers in defined package
final ResourceConfig config = new ResourceConfig().packages(com.myBiz.myWebApp);
return config;
}
/**
* Starts HTTP server exposing JAX-RS resources defined in this application.
* @return HTTP server.
*/
public static HttpServer startServer() {
return JdkHttpServerFactory.createHttpServer(URI.create(BASE_URI), getResources());
}
}
Run Code Online (Sandbox Code Playgroud)
这是关联的build.xml,需要引用此类(WebApplication):
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The following instantiates the class WebApplication, resources are scanned on WebApplication object creation and init is done as well -->
<servlet>
<servlet-name>myWebApp</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.myBiz.myWebApp.WebApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myWebApp</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
从这里,只需创建一个"测试"资源来检查计数器:
package com.myBiz.myWebApp;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.myBiz.myWebApp.WebApplication;
@Path("/test")
public class ResourceTest {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getResource() {
WebApplication.myCounter++;
return "Counter: " + WebApplication.myCounter;
}
}
Run Code Online (Sandbox Code Playgroud)
应该使用值3 + 1初始化计数器,随后刷新资源只会将其增加1.
| 归档时间: |
|
| 查看次数: |
23009 次 |
| 最近记录: |