Nabor
Nabor

Reputation: 1701

CXF return list of elements

This works fine:

@GET
@Path("elements")
public List<Element> getElements(@HeaderParam(SESSION_TOKEN) String token) {
    try {
        if (token != null) {
            Session session = new Session();
            if (session.initWithToken(token)) {
                ElementFacade sf = ElementFacade.getInstance();
                return sf.getElements(session.getUser());
            }
        }
    } catch (Throwable th) {
        log.error("", th);
    }
    return new ArrayList<Element>();
}

I get a very nice JSON array with objects.

The Problem is now I like to put my data as payload into a Response, to be able to set headers and status and to alternatively return an error object as payload. But it doesn't work.

@GET
@Path("elements2")
public Response getElements2(@HeaderParam(SESSION_TOKEN) String token) {
    try {
        if (token == null) {
            return ResponseFactory.createResponse(401, 4007);
        }
        Session session = new Session();
        if (session.initWithToken(token)) {
            ElementFacade sf = ElementFacade.getInstance();
            return Response.status(200)
                    .header(Endpoint.SESSION_TOKEN, session.getToken())
                    .entity(sf.getElements(session.getUser())).build();
        }
        return ResponseFactory.createResponse(403, 4006);
    } catch (InvalidTokenException e) {
        return ResponseFactory.createResponse(401, 4005);
    } catch (SessionTimeoutException e) {
        return ResponseFactory.createResponse(401, 4004);
    } catch (Throwable th) {
        log.error("", th);
        return ResponseFactory.createResponse(500, 5099);
    }
}

Mai 16, 2012 7:00:35 PM org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor writeResponseErrorMessage Warnung: No message body writer has been found for response class ArrayList.

How can I user the same existing functionality that works for the first function in the second function?

I read something about writing an own MessageBodyWriter but in that class I need to write the whole JSON into an OutputStream and seems to be a lot work and overhead...

Thanks in advance

Upvotes: 2

Views: 4826

Answers (1)

Nabor
Nabor

Reputation: 1701

It's so damn easy. But haven't found it in any documentation...

I tried a lot and then baammm...

@GET
@Path("elements2")
public Response getElements2(@HeaderParam(SESSION_TOKEN) String token) {
      try {
          if (token == null) {
              return ResponseFactory.createResponse(401, 4007);
          }
          Session session = new Session();
          if (session.initWithToken(token)) {
              ElementFacade sf = ElementFacade.getInstance();
              List<Element> list = sf.getElements(session.getUser());
              return Response.status(200)
                      .header(Endpoint.SESSION_TOKEN, session.getToken())
                      .entity(new GenericEntity<List<Element>>(list) {}).build();
          }
          return ResponseFactory.createResponse(403, 4006);
      } catch (InvalidTokenException e) {
          return ResponseFactory.createResponse(401, 4005);
      } catch (SessionTimeoutException e) {
          return ResponseFactory.createResponse(401, 4004);
      } catch (Throwable th) {
          log.error("", th);
          return ResponseFactory.createResponse(500, 5099);
      }
}

That's it... You only need to create a GenericEntity...

Upvotes: 1

Related Questions