java - 读取属性 Weblogic Server 时出现 NullpointerException

标签 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)'> <<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 用于所有路径:( 。

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

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

感谢您的帮助,并对我的英语表示抱歉

最佳答案

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

关于java - 读取属性 Weblogic Server 时出现 NullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59258056/

相关文章:

java - createImage(int width, int height) 的问题

jersey - Jersey @Consumes 和 @Produces 之间有什么不同

java - Linux Mint 中的 JAVA 问题

Java - KeyPairGenerator.Initialize(int,SecureRandom)NullPointerException

MongoDb 集合 findAll() 上的 Java NullPointerException

通过 Web 服务返回 JAXB 对象时的 JPA LazyInitializationException

java - 在 java 中使用 Oauth 保护 Rest API

java: NullPointerException,需要帮助

java - Java Records实际上是通过类似的类声明来节省内存还是它们更像语法糖?

java - 在 java 中使用 Keylistener 的 CardLayout 问题