Tomcat 7 Jersey Jackson 200 OK但是有效载荷被剥夺了

时间:2016-10-04 21:35:05

标签: java rest tomcat jersey jackson

我使用Jersey / Jackson进行了休息服务,并且我正在部署到开发中的Tomcat 7。当我使用Jetty时,我打电话

https://localhost/usorgws-web/v1/usorg/search/dep?page=2&limit=10

一切正常,我得到200响应,返回有效负载是一个对象列表。但是,当我部署到我们的开发环境并使用服务器的主机名进行相同的调用替换localhost时,我得到200 Okay,但没有返回有效负载。我确定它正常工作,因为我在代码中放置了打印行并且我正在查看日志。我可以看到有效负载作为填充列表从我的dao一直传播到我的资源。但是看起来,只要tomcat容器获得了响应,它就会因某种原因剥离有效负载。我没有在日志中看到任何错误

以下是一些代码:

资源

import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.util.List;


/**
 * Created by barryj on 9/14/16.
 */
@Path("/usorg")
@Component
public class USOrgResource {

    private final static Logger logger = Logger.getLogger(USOrgResource.class);

    @Autowired
    private USOrgDao usOrgDao;

    @Autowired
    private USOrgService usOrgService;

    @Context
    private UriInfo uriInfo;

    @Value("${pagination.limit.default.min}")
    private int minLimit;

    @Value("${pagination.limit.default.max}")
    private int maxLimit;

    @GET
    @Path("orgCode/{orgCode}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getUSOrgByOrgCode(@PathParam("orgCode") String orgCode) throws
            SystemException {
        USOrg usOrg = null;
        try {
            usOrg = usOrgDao.getUsOrgByOrgCode(orgCode);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new SystemException();
        }
        ResponseWrapper<USOrg> responseWrapper = new ResponseWrapper<>();
        responseWrapper.setData(usOrg);
        responseWrapper.setDescription("USOrg retrieval by org_code");
        responseWrapper.setSuccess(true);
        return Response.status(200).entity(responseWrapper).build();
    }

    @GET
    @Path("search/{term}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response searchUsOrgWithPagination(@PathParam("term") String term, @QueryParam
            ("page") int page, @QueryParam("limit") int limit) throws SystemException {
        logger.info("hit");
        checkPageBounds(limit);
        USOrgPageResults usOrgPageResults = null;
        try {
            usOrgPageResults = usOrgService.searchUsOrg(term, page, limit);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new SystemException();
        }
        ResponseWrapper<List<USOrg>> responseWrapper = new ResponseWrapper<>();
        responseWrapper.setData(usOrgPageResults.getUsOrgList());
        responseWrapper.setDescription("USOrg search");
        responseWrapper.setSuccess(true);

        return buildPaginationResponse(200, responseWrapper, usOrgPageResults);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getUSOrg(@QueryParam("page") int page, @QueryParam("limit") int limit) throws
            SystemException {
        logger.info("hit");
        checkPageBounds(limit);
        USOrgPageResults usOrgPageResults = null;
        try {
            usOrgPageResults = usOrgService.getUsOrg(page, limit);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new SystemException();
        }
        ResponseWrapper<List<USOrg>> responseWrapper = new ResponseWrapper<>();
        responseWrapper.setData(usOrgPageResults.getUsOrgList());
        responseWrapper.setDescription("USOrg search");
        responseWrapper.setSuccess(true);

        if (responseWrapper.getData() != null)
            logger.info("list size: " + responseWrapper.getData().size());
        else
            logger.info("list is null");
        return buildPaginationResponse(200, responseWrapper, usOrgPageResults);
    }

    private Response buildPaginationResponse(int status, Object entity, USOrgPageResults
            usOrgPageResults) {

        final String PAGE = "?page=";
        final String LIMIT = "&limit=";

        String first = "";
        if (usOrgPageResults.getFirst() != null)
            first = uriInfo.getAbsolutePath() + PAGE + usOrgPageResults.getFirst().getPage() +
                    LIMIT + usOrgPageResults.getFirst().getLimit();

        String prev = "";
        if (usOrgPageResults.getSelf().getPage() > 0)
            prev = uriInfo.getAbsolutePath() + PAGE + usOrgPageResults.getPrev()
                    .getPage() + LIMIT + usOrgPageResults.getPrev().getLimit();

        String self = uriInfo.getAbsolutePath() + PAGE + usOrgPageResults.getSelf().getPage() +
                LIMIT + usOrgPageResults.getSelf().getLimit();

        String next = "";
        if (usOrgPageResults.getNext() != null)
            next = uriInfo.getAbsolutePath() + PAGE + usOrgPageResults.getNext().getPage() +
                    LIMIT + usOrgPageResults.getNext().getLimit();

        String last = "";
        if (usOrgPageResults.getLast() != null)
            last = uriInfo.getAbsolutePath() + PAGE + usOrgPageResults.getLast().getPage() +
                    LIMIT + usOrgPageResults.getLast().getLimit();

        return Response.status(status).entity(entity).header("first", first).header
                ("prev", prev).header("self", self).header("next", next).header
                ("last", last).build();
    }

    private void checkPageBounds(int limit) {
        if (limit < minLimit)
            throw new NotAllowedException("The limit requested is below the default limit minimum");
        if (limit > maxLimit)
            throw new NotAllowedException("The limit requested is above the default limit maximum");
    }

    public void setMinLimit(int minLimit) {
        this.minLimit = minLimit;
    }

    public void setMaxLimit(int maxLimit) {
        this.maxLimit = maxLimit;
    }
}

这是我注册JacksonJaxbJsonProvider的WebApplication类

import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
import org.glassfish.jersey.jackson.JacksonFeature;

import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;


public class WebApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> s = new HashSet<Class<?>>();
        s.add(USOrgResource.class);
        s.add(JacksonFeature.class);
        s.add(SystemExceptionMapper.class);
        s.add(NotFoundExceptionMapper.class);
        s.add(NotAllowedExceptionMapper.class);
        s.add(JacksonJaxbJsonProvider.class);
        s.add(CommonResponseHeaderFilter.class);
        return s;
    }
}

1 个答案:

答案 0 :(得分:0)

我还没有深究它,但是当我在WebApplication中注释掉s.add(CommonResponseHeaderFilter.class)行时,当我部署到tomcat时一切正常

public class CommonResponseHeaderFilter implements ContainerResponseFilter {
    public CommonResponseHeaderFilter() {
    }

    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
        responseContext.getHeaders().add("x-content-type-options", "nosniff");
        responseContext.getHeaders().add("Cache-Control", "no-cache");
        responseContext.getHeaders().add("Keep-Alive", "timeout=5, max=100");
        responseContext.getHeaders().add("Connection", "Keep-Alive");
        responseContext.getHeaders().add("Transfer-Encoding", "chunked");
    }
}