问题
I followed the Jersey 2.0 document (https://jersey.java.net/documentation/latest/user-guide.html#json.moxy), modified pom.xml, included jersey-media-moxy artifact, compiled and installed. I could get basic POJO to JSON mapping work for both Produces and Consumes cases.
However, when I tried with some POJO with complex data type as resource return type, I got a lot Status 500 Internal Server Error but without any server log. It is very annoying. Does anybody know if it is a bug or I missed something in configuration?
By the way, in order to use Moxy mapping for a POJO object, the POJO needs to have a empty parameter constructor. Are there any other requirements?
回答1:
I had the same problem and found this post:
- http://jersey.576304.n2.nabble.com/Missing-MOXy-Log-Messages-In-Tomcat-td7580992.html
The author implemented ApplicationEventListener to log the exceptions thrown by Moxy. This is my implementation (needs to be registered in your application):
public class ExceptionListener implements ApplicationEventListener {
@Override
public void onEvent(ApplicationEvent event) {
}
@Override
public RequestEventListener onRequest(RequestEvent requestEvent) {
return new ExceptionRequestEventListener();
}
public static class ExceptionRequestEventListener implements RequestEventListener{
private final Logger logger;
public ExceptionRequestEventListener(){
logger = Logger.getLogger(getClass());
}
@Override
public void onEvent(RequestEvent event) {
switch (event.getType()){
case ON_EXCEPTION:
Throwable t = event.getException();
logger.error("Found exception for requestType: "+event.getType(), t);
}
}
}
}
回答2:
The official Jersey examples that demonstrate how to integrate EclipseLink MOXy can be found at the following link:
- https://github.com/jersey/jersey/tree/master/examples/json-moxy
Where to find the stacktrace for the internal server error will depend upon your setup, can you provide additional information about yours?
Using the above example if I remove the default constructor from the org.glassfish.jersey.examples.jsonmoxy.TestBean
class I will get the following exceptions when running mvn test
. One of them is good and the other is bad. I'm going to follow up with the Jersey lead to work out the proper behaviour.
Bad Exception
testGet(org.glassfish.jersey.examples.jsonmoxy.JsonResourceTest) Time elapsed: 0.507 sec <<< ERROR!
javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:904)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:749)
at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:88)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:650)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:426)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:646)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:375)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:275)
at org.glassfish.jersey.examples.jsonmoxy.JsonResourceTest.testGet(JsonResourceTest.java:76)
Good Exception
roundTripTest(org.glassfish.jersey.examples.jsonmoxy.JsonResourceTest) Time elapsed: 0.048 sec <<< ERROR!
javax.ws.rs.WebApplicationException: HTTP 500 Internal Server Error
at org.eclipse.persistence.exceptions.JAXBException.factoryMethodOrConstructorRequired(JAXBException.java:144)
at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.finalizeProperties(AnnotationsProcessor.java:896)
at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.processClassesAndProperties(AnnotationsProcessor.java:282)
at org.eclipse.persistence.jaxb.compiler.Generator.<init>(Generator.java:150)
at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1017)
at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:174)
at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:165)
at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:152)
at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:112)
at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:102)
at org.eclipse.persistence.jaxb.rs.MOXyJsonProvider.getJAXBContext(MOXyJsonProvider.java:302)
at org.eclipse.persistence.jaxb.rs.MOXyJsonProvider.writeTo(MOXyJsonProvider.java:787)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:194)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139)
at org.glassfish.jersey.filter.LoggingFilter.aroundWriteTo(LoggingFilter.java:268)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139)
at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1005)
at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:430)
at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:290)
at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:203)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:215)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:650)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:426)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:646)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:402)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:305)
at org.glassfish.jersey.examples.jsonmoxy.JsonResourceTest.roundTripTest(JsonResourceTest.java:84)
UPDATE
Starting with EclipseLink 2.5.1 you can use MOXyJsonProvider
standalone to test your model.
import java.lang.reflect.Field;
import java.util.*;
import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider;
public class Demo {
private List<Foo> foos;
public static void main(String[] args) throws Exception {
MOXyJsonProvider moxyJsonProvider = new MOXyJsonProvider();
Field field = Demo.class.getDeclaredField("foos");
List<Foo> foos = new ArrayList<Foo>(1);
Foo foo = new Foo();
foo.setBar("Hello World");
foos.add(foo);
moxyJsonProvider.writeTo(foos, field.getType(), field.getGenericType(), null, null, null, System.out);
}
}
来源:https://stackoverflow.com/questions/17667442/jersey-2-0-and-moxy-internal-server-error-but-no-server-log