[读取属性Weblogic Server时出现NullpointerException

问题描述 投票:0回答:1

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

这是我的所有属性的提取根目录的变量,该变量为null,无法为我的文件.properties设置所有路径。

->另一个应用程序使用相同的方法为weblogic环境提取此变量,并且不会发生此错误

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

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

现在我在本地Weblogic服务器中遇到的错误就像null,但是有所不同,因为en dev environmet null是针对weblogic变量的,而对于此更改,null是针对所有路径的:(。

该应用程序运行正常,但是当重新部署我的war应用程序时有时重新启动服务器时会发生错误。但是使用应用程序时,请从属性返回值。

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

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

java nullpointerexception jax-rs weblogic
1个回答
0
投票

嗯,在创建具有类似配置的项目以免更改正在其他组件上执行的测试之后,解决方案是:从weblogic控制台中删除该组件,重新部署整个组件(war应用程序) 。

© www.soinside.com 2019 - 2024. All rights reserved.