gpt4 book ai didi

java - Jersey CORS 为 GET 而不是 POST 工作

转载 作者:搜寻专家 更新时间:2023-10-30 21:33:30 25 4
gpt4 key购买 nike

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

但是,对以下方法执行 POST 操作会使我最终得到 XMLHttpRequest cannot load http://production.local/api/workstation。 Access-Control-Allow-Origin 不允许来源 http://workstation.local:81。

这是网络 Activity 的屏幕截图: 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;
}
}

我的主类中的 Jersey 配置:

//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/*");

最佳答案

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

org.glassfish.grizzly.servlet.ServletHandler在第 256 行处理异常...

    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)这给了我一些我可以实际使用的东西。

关于java - Jersey CORS 为 GET 而不是 POST 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16718234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com