我在Eclipse中进行了以下设置:
项目A - 用作Web服务代码的动态Web项目。 项目B - 包含Main()的类的测试客户端,以便我可以测试项目A.
项目A包含一个属性文件,当项目A部署为Web服务时,该文件可以正常工作。但是当我从测试客户端项目B运行测试时,无法识别属性文件。我创建了一个名为“resources”的文件夹,它与项目A中的“src”文件夹处于同一级别,此资源文件夹包含属性文件。项目B在其构建路径中包含项目A.
项目B - 客户=)以下是Eclipe中Client项目的示例代码:
public class Match {
public static void main(String[] args) {
MatchService service = new MatchService();
SearviceRequest request = new ServiceRequest();
request.setId(1);
System.out.println("Output -> " + service.process(request));
}
}
Web Project Under Test (Project A):
public class MatchService{
public ServiceResponse process(SearviceRequest request){
ServiceResponse resp = new ServiceResponse();
if (request != null){
ServiceProcessor processor = new ServiceProcessor();
resp = processor.findMatch(request.getID());
}
}
}
public class ServiceProcessor{
public ServiceResponse findMatch(ID id){
if (id != null){
// Read Properties File.
File file = new File("resources/My.properties") <---This fails when code is called from client. (Gives file no found exception. But File is present.
// Connect to database
// Get Data
// Build Response
// Send Response
}
}
}