我已经创建了一个泽西项目的Maven原型球衣-快速启动-网络应用程序在NetBeans上。我使用ActiveJDBC进行数据持久化。在其文档中,可以引用数据库连接属性的外部文件:管理#财产所在地-文件
我遵循了说明,我有这个activejdbc.properties (位于activejdbc.properties文件内容中):
env.connections.file=C:\dev\database.properties和database.properties文件:
development.driver=com.mysql.jdbc.Driver
development.username=user_db
development.password=*****
development.url=jdbc:mysql://192.168.0.19/customersdb
test.driver=com.mysql.jdbc.Driver
test.username=user_db
test.password=*****
test.url=jdbc:mysql://192.168.0.19/customersdb
production.jndi=java:comp/env/jdbc/acme另外,我有一个web服务:
@Path("telephone")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class TelephoneResource {
@GET
public Response get() {
try {
Base.open();
LazyList<Telephone> tels= Telephone.findAll();
String telsJson = tels.toJson(true);
Base.close();
return Response.ok().entity(telsJson).build();
} catch (Exception ex) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("{\"error\": \"" + ex.getMessage() + "\"}").build();
}
}
}但是,当我尝试执行GET资源时,我会收到以下错误消息:
{
"error": "Could not find configuration in a property file for environment: development. Are you sure you have a database.properties file configured?"
}我也尝试过其他的配置:
通过使用pom.xml将它们添加到maven-surefire-plugin文件中
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<systemPropertyVariables>
<activejdbc.url>jdbc:mysql://192.168.0.19/customersdb</activejdbc.url>
<activejdbc.user>user_db</activejdbc.user>
<activejdbc.password>*****</activejdbc.password>
<activejdbc.driver>com.mysql.jdbc.Driver</activejdbc.driver>
</systemPropertyVariables>
<environmentVariables>
<ACTIVEJDBC.URL>jdbc:mysql://192.168.0.19/customersdb</ACTIVEJDBC.URL>
<ACTIVEJDBC.USER>user_db</ACTIVEJDBC.USER>
<ACTIVEJDBC.PASSWORD>*****</ACTIVEJDBC.PASSWORD>
<ACTIVEJDBC.DRIVER>com.mysql.jdbc.Driver</ACTIVEJDBC.DRIVER>
</environmentVariables>
</configuration>
</plugin>但它们也不起作用。
发布于 2018-02-08 18:28:38
此页面:管理#财产所在地-文件声明:
将文件activejdbc.properties添加到类路径根目录下的项目中,并在其中配置一个属性:
这意味着文件需要位于类路径的根目录。因为您使用了Maven,这意味着:
src/main/resources/database.properties看看这个应用程序,包括测试:https://github.com/javalite/simple-example
另外,在控制器内打开一个连接是一个非常糟糕的方法。由于您没有使用ActiveWeb哪个自动管理连接,所以可以编写一个类似于:ActiveJdbcFilter的简单Servlet过滤器,并从资源中提取Base.open()/Base.close()代码:
public class TelephoneResource {
@GET
public Response get() {
try {
return Response.ok().entity(Telephone.findAll().toJson(true)).build();
} catch (Exception ex) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("{\"error\": \"" + ex.getMessage() + "\"}").build();
}
}
}如果您愿意探索ActiveWeb,下面是Rest Rest应用程序的一个简单示例:https://github.com/javalite/activeweb-rest
https://stackoverflow.com/questions/48691163
复制相似问题