exception in parsing the request after pre-flight

2019-07-28 06:25发布

问题:

  1. We have created the CORS filter as follows:

    @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. We have created a REST resource

    Following is the code:

    @Path("MyResource")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String MyResource(TestInput input) {
        return input.getName();
    }
    
  3. The class TestInput is as follows:

    public class TestInput {
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
  4. It produces the result as expected from Postman. But if we hit the resource from http://resttesttest.com/ or any client, following error occurs:

    javax.servlet.ServletException: javax.json.stream.JsonParsingException: Unexpected char 117 at (line no=1, column no=1, offset=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)

    Root Cause

    javax.json.stream.JsonParsingException: Unexpected char 117 at (line no=1, column no=1, offset=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. preflight request is handled by returning if cors header is present.

Please guide me on this. Thanks in advance.

回答1:

I think there's nothing wrong with your ContainerResquestFilter. What you need to do only update your TestInput class and add the following annotation on it.

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

    @XmlElement
    private String name;

    public String getName() {
        return name;
    }

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