【问题标题】:NullpointerException when reading properties Weblogic Server读取属性 Weblogic Server 时出现 NullpointerException
【发布时间】:2019-12-09 23:23:10
【问题描述】:

我在 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)'> <<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)

我调用属性文件的代码是使用 JAXRS 的 Java 代码:

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 );
    }
  }
}

war 应用程序部署在 Weblogic 服务器中时,日志中显示错误。但是当我在自己的机器上部署 Weblogic 12.2.1 服务器时,就可以正常工作了。

错误日志中检测问题的行是:

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)

这个用于提取我所有属性的根目录的变量为空,并且不能使我的文件.properties 的所有路径。

-->另一个应用程序使用相同的方法为weblogic环境提取这个变量,这个错误没有发生

为了尝试在我的机器上重新创建错误,我只是这样做了: (在创建 FileInputStream 的行中:

// is = new FileInputStream( urlServer );
    //change for :
   is = this.getClass().getClassLoader().getResourceAsStream( urlServer );

现在我的本地 Weblogic 服务器中的错误类似于 null,但不同,因为 en dev environmet null 适用于 weblogic 变量,而对于此更改,null 适用于所有路径:(。

应用程序工作正常,但有时在重新部署我的战争应用程序时重新启动服务器时会发生错误。但是在使用应用程序时,从属性中返回值。

我认为使用 JAXRS 时 REST 应用程序的生命周期,但我是 JavaEE 的新手。

感谢您的帮助,对不起我的英语

【问题讨论】:

  • 这似乎有点傻,但您的属性文件是否位于my-directory/.properties
  • @randypaq13 是的,因为我所说的应用程序运行良好,从 .properties 文件返回值
  • 如果文件在那里,我不可能从这里调试。也许尝试使缓存无效并重新启动您的IDE?该错误表明该文件不存在,因此程序没有在正确的目录中看到该文件。抱歉,我无法给出更完整的答案!

标签: java nullpointerexception jax-rs weblogic


【解决方案1】:

好吧,在创建了一个具有类似配置的项目之后,为了不改变正在对另一个组件执行的测试,解决方案是:从 weblogic 控制台中删除该组件,重新部署整个组件(战争应用程序)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    相关资源
    最近更新 更多