WeldProxy在异常中无法识别源。

huangapple go评论76阅读模式
英文:

WeldProxy doesn't recognize source on exception

问题

我在一个使用Jax-RSCDI并运行在Wildfly 14上的Java 8应用程序中遇到了一个奇怪的问题

我有这个简单的Controller类

@Path("/test")
public class TestController {
    private final static Logger logger = LogManager.getLogger();

    @GET
    @Path("/test")
    @Produces(MediaType.APPLICATION_JSON)
    public Response test(@Context SecurityContext request) {
        try (InputStream is = new FileInputStream("properties.txt")) {
            System.getProperties().load(is);
        } catch (Exception e) {
            logger.error("Error", e);
        }

        return Response.ok().build();
    }
}

调用test方法总是会抛出一个异常因为properties.txt不存在这是正常的奇怪的是在堆栈跟踪中有很多未知来源

[ERROR] [/test/test] [rest.TestController] 发生错误
java.io.FileNotFoundException: properties.txt
at java.io.FileInputStream.open0(Native Method) ~[?:1.8.0_252]
at java.io.FileInputStream.open(FileInputStream.java:195) ~[?:1.8.0_252]
at java.io.FileInputStream.<init>(FileInputStream.java:138) ~[?:1.8.0_252]
at java.io.FileInputStream.<init>(FileInputStream.java:93) ~[?:1.8.0_252]
at com.testapp.rest.TestController.test(TestController.java:42) ~[classes:?]
**at com.testapp.rest.TestController$Proxy$_$$_WeldClientProxy.test(Unknown Source) ~[classes:?]**
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_252]
[...]

粗体行是我不理解的一行也许是Wildfly的配置有问题

顺便说一下我的Jax-RS初始化器是这样的

@ApplicationPath("/rest")
public class JaxRsActivator extends Application {

    private static final Logger logger = LogManager.getLogger();

    private Set<Object> singletons = new HashSet<Object>();
    private HashSet<Class<?>> classes = new HashSet<Class<?>>();

    public JaxRsActivator() {
        LogFactory.useLog4J2Logging();

        classes.add(TestController.class);
    }

    @Override
    public Set<Object> getSingletons() {
        return singletons;
    }

    @Override
    public HashSet<Class<?>> getClasses(){
      return classes;
    }
}

这是我的log4j2.xml配置文件

<Configuration status="WARN">
    <Properties>
        <Property name="basePath">${logDir}</Property>
        <Property name="messagePattern">[%-5level] %X{method}[%logger{2}] %msg</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="${messagePattern}\n" />
        </Console>

        <RollingFile name="File" fileName="${basePath}/app.log" filePattern="${basePath}/app-%d{yyyy-MM-dd}-%i.log" ignoreExceptions="false">
            <PatternLayout>
                <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ${messagePattern}%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" />
            </Policies>
            <DefaultRolloverStrategy max="5" />
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console" />
            <AppenderRef ref="File" />
        </Root>
        <Logger name="org.apache.ibatis.transaction.jdbc.JdbcTransaction" level="ERROR" additivity="false">
            <Appender-ref ref="File" level="ERROR" />
            <Appender-ref ref="Console" level="ERROR" />
        </Logger>
        <Logger name="org.apache.ibatis.io" level="ERROR">
            <Appender-ref ref="File" />
            <Appender-ref ref="Console"/>
        </Logger>
        <Logger name="org.jboss.resteasy.resteasy_jaxrs.i18n" level="ERROR" additivity="false">
            <Appender-ref ref="File" level="ERROR" />
            <Appender-ref ref="Console" level="ERROR" />
        </Logger>
    </Loggers>
</Configuration>
英文:

I'm facing a strange issue in a Java 8 application which use Jax-RS, CDI and run on Wildfly 14.

I have this simple Controller class:

Path(&quot;/test&quot;)
public class TestController {
private final static Logger logger = LogManager.getLogger();
@GET
@Path(&quot;/test&quot;)
@Produces(MediaType.APPLICATION_JSON)
public Response test(@Context SecurityContext request) {
try (InputStream is = new FileInputStream(&quot;properties.txt&quot;)) {
System.getProperties().load(is);
} catch (Exception e) {
logger.error(&quot;Error&quot;, e);
}
return Response.ok().build();
}

Calling test method will always throw an Exception because properties.txt doesn't exists and this is ok. The strange is that in the stack trace I have a lot of unknown source:

[ERROR] [/test/test] [rest.TestController] Error occurs
java.io.FileNotFoundException: properties.txt
at java.io.FileInputStream.open0(Native Method) ~[?:1.8.0_252]
at java.io.FileInputStream.open(FileInputStream.java:195) ~[?:1.8.0_252]
at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:138) ~[?:1.8.0_252]
at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:93) ~[?:1.8.0_252]
at com.testapp.rest.TestController.test(TestController.java:42) ~[classes:?]
**at com.testapp.rest.TestController$Proxy$_$$_WeldClientProxy.test(Unknown Source) ~[classes:?]**
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_252]
[...]

The bold line is the one I'm not understanding. Maybe is a Wildfly bad configuration?

By the way, my Jax-RS initializer is this:

@ApplicationPath(&quot;/rest&quot;)
public class JaxRsActivator extends Application {
private static final Logger logger = LogManager.getLogger();
private Set&lt;Object&gt; singletons = new HashSet&lt;Object&gt;();
private HashSet&lt;Class&lt;?&gt;&gt; classes = new HashSet&lt;Class&lt;?&gt;&gt;();
public JaxRsActivator() {
LogFactory.useLog4J2Logging();
classes.add(TestController.class);
}	
@Override
public Set&lt;Object&gt; getSingletons() {
return singletons;
}
@Override
public HashSet&lt;Class&lt;?&gt;&gt; getClasses(){
return classes;
}
}

And this is my log4j2.xml

&lt;Configuration status=&quot;WARN&quot;&gt;
&lt;Properties&gt;
&lt;Property name=&quot;basePath&quot;&gt;${logDir}&lt;/Property&gt;
&lt;Property name=&quot;messagePattern&quot;&gt;[%-5level] %X{method}[%logger{2}] %msg&lt;/Property&gt;
&lt;/Properties&gt;
&lt;Appenders&gt;
&lt;Console name=&quot;Console&quot; target=&quot;SYSTEM_OUT&quot;&gt;
&lt;PatternLayout pattern=&quot;${messagePattern}\n&quot; /&gt;
&lt;/Console&gt;
&lt;RollingFile name=&quot;File&quot; fileName=&quot;${basePath}/app.log&quot; filePattern=&quot;${basePath}/app-%d{yyyy-MM-dd}-%i.log&quot; ignoreExceptions=&quot;false&quot;&gt;
&lt;PatternLayout&gt;
&lt;Pattern&gt;%d{yyyy-MM-dd HH:mm:ss.SSS} ${messagePattern}%n&lt;/Pattern&gt;
&lt;/PatternLayout&gt;
&lt;Policies&gt;
&lt;TimeBasedTriggeringPolicy interval=&quot;1&quot; /&gt;
&lt;/Policies&gt;
&lt;DefaultRolloverStrategy max=&quot;5&quot; /&gt;
&lt;/RollingFile&gt;
&lt;/Appenders&gt;
&lt;Loggers&gt;
&lt;Root level=&quot;debug&quot;&gt;
&lt;AppenderRef ref=&quot;Console&quot; /&gt;
&lt;AppenderRef ref=&quot;File&quot; /&gt;
&lt;/Root&gt;
&lt;Logger name=&quot;org.apache.ibatis.transaction.jdbc.JdbcTransaction&quot; level=&quot;ERROR&quot; additivity=&quot;false&quot;&gt;
&lt;Appender-ref ref=&quot;File&quot; level=&quot;ERROR&quot; /&gt;
&lt;Appender-ref ref=&quot;Console&quot; level=&quot;ERROR&quot; /&gt;
&lt;/Logger&gt;
&lt;Logger name=&quot;org.apache.ibatis.io&quot; level=&quot;ERROR&quot;&gt;
&lt;Appender-ref ref=&quot;File&quot; /&gt;
&lt;Appender-ref ref=&quot;Console&quot;/&gt;
&lt;/Logger&gt;
&lt;Logger name=&quot;org.jboss.resteasy.resteasy_jaxrs.i18n&quot; level=&quot;ERROR&quot; additivity=&quot;false&quot;&gt;
&lt;Appender-ref ref=&quot;File&quot; level=&quot;ERROR&quot; /&gt;
&lt;Appender-ref ref=&quot;Console&quot; level=&quot;ERROR&quot; /&gt;
&lt;/Logger&gt;
&lt;/Loggers&gt;
&lt;/Configuration&gt;

答案1

得分: 2

以上有几条评论提供了正确的答案:不管出于什么原因,Weld决定为您的资源类创建客户端代理。 客户端代理源代码通常是不可用的。

现在,真正的问题是:为什么Weld要为您的资源类创建客户端代理,毕竟它上面没有范围注解,因此您会认为它应该是@Dependent作用域?我猜测RestEasy,这是由Red Hat编写的JAX-RS实现,并且我假设它作为WildFly的一部分进行了发布,它会默认将未注释的资源类设置为@RequestScoped,而不像Jersey那样,最接近@Dependent作用域的效果。而且,最重要的是:@RequestScoped对象是经过代理的。

英文:

A couple of the comments above have the right answer: for whatever reason, Weld has seen fit to create a client proxy for your resource class. Client proxy sources are not (as a rule) available.

Now, the real question is: why is Weld creating a client proxy for your resource class, given that it has no scope annotation on it, and therefore, you'd think, would be in @Dependent scope? I'm going to guess that RestEasy, the JAX-RS implementation authored by Red Hat and, I assume, shipped as part of WildFly, makes unannotated resource classes @RequestScoped by default, rather than what Jersey does, which is most closely emulated by @Dependent scope. And, most importantly: @RequestScoped objects are proxied.

答案2

得分: -1

关于 Weld 的问题与 Weld 本身无关,而是与我的异常层次结构有关,这导致堆栈跟踪丢失。

英文:

My "issue" with Weld it's not related with Weld but it's a fault in my Exception hierachy which cause a loss in stack trace.

huangapple
  • 本文由 发表于 2020年7月27日 17:33:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63112479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定