Jersey returns 500 when trying to return an XML response

前端 未结 4 1199
闹比i
闹比i 2021-01-20 03:56

I\'m trying to create my own RESTful WS application using Jersey 2.12 based from this article. I want to return an XML representation of a class depending on the id been pas

相关标签:
4条回答
  • 2021-01-20 04:30

    Stuff below is not the answer, but will probably help Johne to find out, whats up.

    Out of the comments I've extract, that the main problem is, that you don't have any noticeable debug output in your console. So you are not able to find the issue by yourself, rather than give us some logs which would help to find out, what the exact problem could be.

    Therefore pls implement an ExceptionMapper first, which will force console output of the stacktrace:

    Example:

    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Response;
    import javax.ws.rs.core.Response.Status;
    import javax.ws.rs.ext.ExceptionMapper;
    import javax.ws.rs.ext.Provider;
    
    @Provider
    public class HelpMeExceptionMapper implements ExceptionMapper<Exception> {
    
        @Override
        public Response toResponse(Exception e) {
            e.printStackTrace();
            return Response
                        .status(Status.INTERNAL_SERVER_ERROR)
                        .type(MediaType.APPLICATION_JSON)
                        .entity(e.getCause())
                        .build();
        }
    
    }
    

    The ExceptionMapper has to be in a subpackage of your resource-config/providers path test.services:

    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>test.services</param-value>
    </init-param>
    

    As i didn't implement your/vogellas code i would like to recommend you, to debug this step by step to find out whats up.

    I reckon, that you miss to import something. But who knows ...

    Have a nice day ...

    0 讨论(0)
  • 2021-01-20 04:33

    To the ones with the same problem, I had something like it but in my case it was a List that i wanted in my response. The source of the problem was that the object had a lazy load relationship and when I used GenericEntity> to return my list the same problem as occurred to me. Just change to null the relationship or bring the lazy load relation to the object before create GenericEntity> and it will be fine.

    0 讨论(0)
  • 2021-01-20 04:44

    We need to understand this problem first Jersey returns 500 when trying to return an XML response. This issue is coming because of missing dependency in pom.xml and that is:

    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
        <version>2.2.11</version>
    </dependency>  
    

    But this is not enough. This will resolve the 500 Error but one more error will come, while using endpoint.

    javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: com/sun/xml/bind/v2/model/annotation/AnnotationReader
    

    To resolve this you need to add following dependencies too into pom.xml

    <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-core -->
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>2.3.0.1</version>
    </dependency>
    
            <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl -->
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.1</version>
    </dependency>
    

    So this is the complete solution, to make your Jersey webApp work

    0 讨论(0)
  • 2021-01-20 04:51

    This happens when you don't provide a default no arg constructor to your JAXB bean. So in your case you should amend the class by adding one:

      public TestRestModel(){
    
        }
    

    This is due to a requirement in the JSR-222:

    existing types, authored by users, are required to provide a no arg constructor. The no arg constructor is used by an unmarshaller during unmarshalling to create an instance of the type.

    0 讨论(0)
提交回复
热议问题