Java 读取Weblogic Server属性时出现NullpointerException
我在Weblogic 12.2.1中遇到了这个问题: 从检测到问题的日志中提取[来自服务器的日志]Java 读取Weblogic Server属性时出现NullpointerException,java,nullpointerexception,jax-rs,weblogic,Java,Nullpointerexception,Jax Rs,Weblogic,我在Weblogic 12.2.1中遇到了这个问题: 从检测到问题的日志中提取[来自服务器的日志] ####<06/12/2019 06:01:31 PM COT> <Info> <Deployer> <LIM-4WZN2X2> <AdminServer> <[ACTIVE] ExecuteThread: '2' for queue: 'weblogic.kernel.Default (self-tuning)'> <
####<06/12/2019 06:01:31 PM COT> <Info> <Deployer> <LIM-4WZN2X2> <AdminServer> <[ACTIVE] ExecuteThread: '2' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <ffc7c770-2d51-4047-b19f-51eb060c58fa-0000000a> <1575673291126> <[severity-value: 64] [rid: 0] [partition-id: 0] [partition-name: DOMAIN] > <BEA-149060> <Module demo-resource-1.0.0.war of application demo-resource-1.0.0 successfully transitioned from STATE_PREPARED to STATE_ADMIN on server AdminServer.>
####<06/12/2019 06:01:31 PM COT> <Error> <HTTP> <LIM-4WZN2X2> <AdminServer> <[ACTIVE] ExecuteThread: '2' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <ffc7c770-2d51-4047-b19f-51eb060c58fa-0000000a> <1575673291485> <[severity-value: 8] [rid: 0] [partition-id: 0] [partition-name: DOMAIN] > <BEA-101216> <Servlet: "pe.demo.app.resource.conf.ApplicationConfig" failed to preload on startup in Web application: "demo-resource-1.0.0.war".
pe.demo.app.resource.exception.GeneralRuntimeException: Cannot read .properties file
at pe.demo.app.resource.conf.ApplicationConfig.readProperties(ApplicationConfig.java:78)
at pe.demo.app.resource.conf.ApplicationConfig.getProperties(ApplicationConfig.java:45)
at org.glassfish.jersey.server.ResourceConfig$WrappingResourceConfig.mergeApplications(ResourceConfig.java:1140)
at org.glassfish.jersey.server.ResourceConfig$WrappingResourceConfig._setApplication(ResourceConfig.java:1077)
at org.glassfish.jersey.server.ResourceConfig.setApplication(ResourceConfig.java:1029)
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:342)
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:390)
现在我在本地Weblogic服务器中发现了一个类似于null的错误,但这是不同的,因为en dev environmet null代表Weblogic变量,而对于这个更改,null代表所有路径:(
应用程序工作正常,但在重新部署我的war应用程序时,有时重新启动服务器时会发生错误。但在使用应用程序时,请从属性返回值
我认为当使用JAXRS时,REST应用程序的生命周期很短,但我对JavaEE是新手
感谢您的帮助,并为我的英语感到抱歉。好吧,在创建了一个具有类似配置的项目之后,为了不改变在另一个组件上执行的测试,解决方案是:从weblogic控制台删除该组件,重新部署整个组件(war应用程序).问这个问题似乎有点傻,但您的属性文件是否位于我的目录下?@randypaq13是的,因为应用程序工作正常。我说,从.properties文件返回值如果文件在那里,我就无法从这里调试。可能尝试使缓存无效并重新启动IDE?错误是文件不在那里,因此程序无法在正确的目录中看到文件。很抱歉,我无法给出更完整的答案!
package pe.demo.app.resource.conf;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;
import javax.inject.Singleton;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import pe.demo.app.common.exception.ProviderExceptionMapper;
import pe.demo.app.common.property.Constantes;
import pe.demo.app.common.resource.exception.GeneralRuntimeException;
import pe.demo.app.resource.DemoResource;
@Singleton
@ApplicationPath( "api" )
public class ApplicationConfig extends Application{
private static final String NAME_DIRECTORY = "my-directory";
private static final String NAME_VARIABLE = "variable-weblogic-properties-root";
private static final String NAME_FILE = ".properties";
@Override
public Set<Class<?>> getClasses(){
Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
resources.add( DemoResource.class );
resources.add( ProviderExceptionMapper.class );
resources.add( com.wordnik.swagger.jaxrs.listing.ApiListingResource.class );
resources.add( com.wordnik.swagger.jaxrs.listing.ApiDeclarationProvider.class );
resources.add( com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON.class );
resources.add( com.wordnik.swagger.jaxrs.listing.ResourceListingProvider.class );
return resources;
}
public Map<String, Object> getProperties(){
String nombrePropertieExterno = NAME_FILE;
Map<String, Object> dataProperties = new HashMap<String, Object>();
dataProperties.putAll( readProperties( nombrePropertieExterno, false ) );
return dataProperties;
}
private Map<String, Object> readProperties( String fileInClasspath, Boolean interno ){
System.out.println( "method:[readProperties], nombre de properties recibido:-->" + fileInClasspath );
InputStream is = null;
String urlServer = "";
try{
if( interno ){
is = this.getClass().getClassLoader().getResourceAsStream( fileInClasspath );
}
else{
String filePropertiesRoot = System.getProperty( NAME_VARIABLE );
urlServer = filePropertiesRoot + NAME_DIRECTORY + File.separator + fileInClasspath;
is = new FileInputStream( urlServer );
}
Map<String, Object> map = new HashMap<String, Object>();
Properties properties = new Properties();
properties.load( is );
map.putAll(
properties.entrySet().stream().collect( Collectors.toMap( e -> e.getKey().toString(), e -> e.getValue() ) ) );
is.close();
return map;
}
catch( Exception e ){
throw new GeneralRuntimeException( " Cannot read file " + fileInClasspath, e );
}
}
}
Caused By: java.io.FileNotFoundException: nullmy-directory/.properties (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
// is = new FileInputStream( urlServer );
//change for :
is = this.getClass().getClassLoader().getResourceAsStream( urlServer );