使用球衣

时间:2018-05-09 09:52:24

标签: rest web-services cors jersey-2.0

  1. 我们创建了CORS过滤器,如下所示:

    @Provider
    public class CORSFilter implements ContainerResponseFilter {
    
        @Override
        public void filter(ContainerRequestContext request,
            ContainerResponseContext response) throws IOException {
    
            // TODO: Allow only from *our* Web front 
            response.getHeaders().add("Access-Control-Allow-Origin", "*");
            response.getHeaders().add("Access-Control-Allow-Headers",
                "origin, content-type, accept,x-request-id, x-api-key, x-api-secret, authorization");
            response.getHeaders().add("Access-Control-Allow-Credentials", "true");
            response.getHeaders().add("Access-Control-Allow-Methods",
                "GET, POST, PUT, DELETE, OPTIONS, HEAD");
        }
    }
    
  2. 我们已经创建了一个REST资源

    以下是代码:

    @Path("MyResource")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String MyResource(TestInput input) {
        return input.getName();
    }
    
  3. TestInput类如下:

    public class TestInput {
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
  4. 它会产生Postman所期望的结果。但是,如果我们从http://resttesttest.com/或任何客户端点击资源,则会发生以下错误:

      

    javax.servlet.ServletException:javax.json.stream.JsonParsingException:意外的char 117 at(行号= 1,列号= 1,偏移量= 0)       org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:432)       org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:370)       org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)       org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)       org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)       org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)   

    根本原因

    javax.json.stream.JsonParsingException:意外的char 117 at(行号= 1,列号= 1,偏移量= 0)       org.glassfish.json.JsonTokenizer.unexpectedChar(JsonTokenizer.java:601)       org.glassfish.json.JsonTokenizer.nextToken(JsonTokenizer.java:418)       org.glassfish.json.JsonParserImpl $ NoneContext.getNextEvent(JsonParserImpl.java:413)       org.glassfish.json.JsonParserImpl.next(JsonParserImpl.java:363)       org.eclipse.yasson.internal.JsonbRiParser.next(JsonbRiParser.java:140)       org.eclipse.yasson.internal.Unmarshaller.getRootEvent(Unmarshaller.java:66)       org.eclipse.yasson.internal.Unmarshaller.deserializeItem(Unmarshaller.java:56)       org.eclipse.yasson.internal.Unmarshaller.deserialize(Unmarshaller.java:50)       org.eclipse.yasson.internal.JsonBinding.deserialize(JsonBinding.java:45)       org.eclipse.yasson.internal.JsonBinding.fromJson(JsonBinding.java:85)       org.glassfish.jersey.jsonb.internal.JsonBindingProvider.readFrom(JsonBindingProvider.java:99)       org.glassfish.jersey.message.internal.ReaderInterceptorExecutor $ TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:257)       org.glassfish.jersey.message.internal.ReaderInterceptorExecutor $ TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:236)       org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:156)       org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundReadFrom(MappableExceptionWrapperInterceptor.java:73)       org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:156)       org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1091)       org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:874)       org.glassfish.jersey.server.ContainerRequest.readEntity(ContainerRequest.java:271)       org.glassfish.jersey.server.internal.inject.EntityParamValueParamProvider $ EntityValueSupplier.apply(EntityParamValueParamProvider.java:97)       org.glassfish.jersey.server.internal.inject.EntityParamValueParamProvider $ EntityValueSupplier.apply(EntityParamValueParamProvider.java:80)       org.glassfish.jersey.server.spi.internal.ParamValueFactoryWithSource.apply(ParamValueFactoryWithSource.java:74)       org.glassfish.jersey.server.spi.internal.ParameterValueHelper.getParameterValues(ParameterValueHelper.java:92)       org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider $ AbstractMethodParamInvoker.getParamValues(JavaResourceMethodDispatcherProvider.java:133)       org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider $ ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:200)       org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:103)       org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:493)       org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:415)       org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:104)       org.glassfish.jersey.server.ServerRuntime $ 1.run(ServerRuntime.java:277)       org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:272)       org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:268)       org.glassfish.jersey.internal.Errors.process(Errors.java:316)       org.glassfish.jersey.internal.Errors.process(Errors.java:298)       org.glassfish.jersey.internal.Errors.process(Errors.java:268)       org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:289)       org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:256)       org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:703)       org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:416)       org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:370)       org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)       org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)       org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)       org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

  5. 如果存在cors标头,则返回处理预检请求。

  6. 请指导我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我认为您的ContainerResquestFilter没问题。您只需要更新您的 TestInput 类并在其上添加以下注释即可。

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class TestInput {

    @XmlElement
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}