Java Web服务JAX-RS多线程:传递信息
我正在使用以下配置构建基于JAX-RS的web服务:Java Web服务JAX-RS多线程:传递信息,java,multithreading,web-services,jersey,stateless,Java,Multithreading,Web Services,Jersey,Stateless,我正在使用以下配置构建基于JAX-RS的web服务: IDE:Eclipse 构建:Maven 部署:Tomcat 使用Jersey 1.8的Jax RS注释 DB:mongoDB 3.0带java驱动程序(本地主机) 客户端:具有多个表单的HTML页面,同时处理GET和POST方法 实际上,该程序是围绕一个带有jax-rs注释的java主servlet开发的。这是我的应用程序处理服务器响应/请求的“入口点”。总体目标是实现一个包含多个html页面的web项目,仅使用JAX-RS控制器 我面
- IDE:Eclipse
- 构建:Maven
- 部署:Tomcat
- 使用Jersey 1.8的Jax RS注释
- DB:mongoDB 3.0带java驱动程序(本地主机)
- 客户端:具有多个表单的HTML页面,同时处理GET和POST方法
@Path(“/rest”)
公共类后台应用程序
{
私有静态字符串[]defaultParamList={“时间戳”、“plcVersion”、“udmVersion”、“iotVersion”、“deviceName”、“制造商”、“型号”、“sdk”、“原语名”、“结果”、“状态”};
私有静态字符串[]userParamList={};
私有静态ArrayList容器;
私有静态java.nio.file.Path javaNIOPath;
私有静态MongoClient连接字符串=新MongoClient(“mongodb://localhost:27017");
私人最终MongoClient MongoClient;
公共后端应用程序()
{
此(新MongoClient(connectionString));
}
@职位
@Path(“/postList”)//此上下文开头出现的第一个pot(根目录下的index.html)
@生成(“文本/html”)
公共可查看的getParamList(@FormParam(“textarea”)字符串textarea)引发IOException
{
userParamList=textarea.replaceAll(“\\s*\\n\\s*”,”).split(“,”;//正则表达式过滤所有(多个)空格和回车
返回新的可查看文件(“/query.html”);
}
@得到
@路径(“/result”)
@生成(“文本/html”)
公共响应getHTML(@Context UriInfo UriInfo)引发IOException
{
MongoDatabase mongoDB=mongoClient.getDatabase(“JSONRepository”);
MongoCollection coll=mongoDB.getCollection(“TestColl”);
container=mongo.getParameters(userParamList、coll、queryParams);
//userParamList在这里是静态的。。。
//拿东西。。。。。
}
}
谢谢你的回答
PS:静态内容可以很好地工作……所以问题更多地集中在Jersey的多线程问题上。您确定使用这种方法,因为您想要的是类似web应用程序的东西,并且您尝试使用web服务来实现这一点,在同时有多个请求的情况下,您尝试使用静态内容将不起作用事实上:)我的要求是只使用Jax-Rs注释。它将简化整个项目,只需用一种主要方法处理请求。你还在想什么?好的。我改变了我的方式解决了这个问题。现在可以使用jQuery检索来自HTML页面的参数。因此,不再需要在我的JAX-RSservlet中使用静态内容。
@Path("/rest")
public class BackendApp
{
private static String[] defaultParamList = {"timestamp","plcVersion","udmVersion","iotVersion","deviceName","manufacturer","model","sdk","primitiveName","result","status"};
private static String[] userParamList = {};
private static ArrayList<Document> container;
private static java.nio.file.Path javaNIOPath;
private static MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017");
private final MongoClient mongoClient;
public BackendApp()
{
this(new MongoClient(connectionString));
}
@POST
@Path("/postList") // FIRST pot that occurs at the beginning of this context (index.html at the root)
@Produces("text/html")
public Viewable getParamList(@FormParam("textarea") String textarea) throws IOException
{
userParamList = textarea.replaceAll("\\s*\\n\\s*","").split(","); // REGEX expression filtering all the (multiple) blank spaces and carriage return
return new Viewable("/query.html");
}
@GET
@Path("/result")
@Produces("text/html")
public Response getHTML(@Context UriInfo uriInfo) throws IOException
{
MongoDatabase mongoDB = mongoClient.getDatabase("JSONRepository");
MongoCollection<Document> coll = mongoDB.getCollection("TestColl");
container = mongo.getParameters(userParamList, coll, queryParams);
// userParamList is here static...
// GET STUFF.....
}
}