Jersey CORS为GET而不是POST工作

时间:2013-05-23 15:38:49

标签: java jersey

我的泽西CORS请求不适用于POST,但适用于GET请求。标头被映射到Jersey请求,如下面对同一资源的GET请求的屏幕截图所示。

但是,对以下方法执行POST操作会使我最终得到XMLHttpRequest cannot load http://production.local/api/workstation. Origin http://workstation.local:81 is not allowed by Access-Control-Allow-Origin.

以下是网络活动的屏幕截图: enter image description here

POST请求失败的详细信息: enter image description here

这是我的资源:

@Path("/workstation")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public class WorkstationResource {

    @InjectParam
    WorkstationService workstationService;

    @POST
    public WorkstationEntity save (WorkstationEntity workstationEntity) {
        workstationService.save(workstationEntity);
        return workstationEntity;
    }

    @GET
    @Path("/getAllActive")
    public Collection<WorkflowEntity> getActive () {
        List<WorkflowEntity> workflowEntities = new ArrayList<WorkflowEntity>();
        for(Workflow workflow : Production.getWorkflowList()) {
            workflowEntities.add(workflow.getEntity());
        }
        return workflowEntities;
    }
}

我的CORS过滤器:

public class ResponseCorsFilter implements ContainerResponseFilter {

    @Override
    public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {

        Response.ResponseBuilder responseBuilder = Response.fromResponse(response.getResponse());
        responseBuilder
                .header("Access-Control-Allow-Origin", "*")
                .header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, HEAD");

        String reqHead = request.getHeaderValue("Access-Control-Request-Headers");

        if(null != reqHead && !reqHead.equals(null)){
            responseBuilder.header("Access-Control-Allow-Headers", reqHead);
        }

        response.setResponse(responseBuilder.build());

        return response;
    }
}

我的主要课程中的我的泽西配置:

//add jersey servlet support
ServletRegistration jerseyServletRegistration = ctx.addServlet("JerseyServlet", new SpringServlet());
jerseyServletRegistration.setInitParameter("com.sun.jersey.config.property.packages", "com.production.resource");
jerseyServletRegistration.setInitParameter("com.sun.jersey.spi.container.ContainerResponseFilters", "com.production.resource.ResponseCorsFilter");
jerseyServletRegistration.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", Boolean.TRUE.toString());
jerseyServletRegistration.setInitParameter("com.sun.jersey.config.feature.DisableWADL", Boolean.TRUE.toString());
jerseyServletRegistration.setLoadOnStartup(1);
jerseyServletRegistration.addMapping("/api/*");

2 个答案:

答案 0 :(得分:3)

虽然我认为这是一个CORS问题,但事实证明这是泽西岛问题......

第256行的

org.glassfish.grizzly.servlet.ServletHandler处理异常...

    FilterChainInvoker filterChain = getFilterChain(request);
    if (filterChain != null) {
        filterChain.invokeFilterChain(servletRequest, servletResponse);
    } else {
        servletInstance.service(servletRequest, servletResponse);
    }
} catch (Throwable ex) {
    LOGGER.log(Level.SEVERE, "service exception:", ex);
    customizeErrorPage(response, "Internal Error", 500);
}

在我的日志中,我看到的只是service exception:,后面没有任何内容。当我调试这一行时,我最终看到错误javax.servlet.ServletException: org.codehaus.jackson.map.JsonMappingException: Conflicting setter definitions for property "workflowProcess": com.production.model.entity.WorkstationEntity#setWorkflowProcess(1 params) vs com.production.model.entity.WorkstationEntity#setWorkflowProcess(1 params),它给了我一些我可以实际使用的东西。

答案 1 :(得分:1)

很难说并且难以调试,因为它是浏览器在检查响应(标题)时产生错误。

即使非常仔细地检查,您的代码看起来也很合理,除了Access-Control-Allow-Headersfilter()或者可以在{{1}}中设置两次。虽然RFC 2616 (HTTP 1.1) Section 4.2基本上允许它,但在满足某些条件的情况下,我不会在这里赌博。您无法控制浏览器X版本N如何处理此问题。

不是使用不同的值设置两次相同的标题,而是将第二组值附加到现有标题。