英文:
WeldProxy doesn't recognize source on exception
问题
我在一个使用Jax-RS、CDI并运行在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("/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();
}
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.<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]
[...]
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("/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;
}
}
And this is my 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>
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论