java.lang.IllegalArgumentException:java.text.ParseException:标头结尾 [英] java.lang.IllegalArgumentException: java.text.ParseException: End of header

查看:119
本文介绍了java.lang.IllegalArgumentException:java.text.ParseException:标头结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在向宁静的Web服务发送发布请求时遇到了IllegalArgumentException. 如果我评论发布请求(getEmplByPostReqParam())的方法,则代码对于get请求可以正常工作,但是使用getEmplByPostReqParam()时,所有请求都抛出IllegalArgumentException.

I encounter IllegalArgumentException while sending a post request to the restful web-service. If i comment the method for post request(getEmplByPostReqParam()) then, code works fine for get request but with getEmplByPostReqParam(), all request throws IllegalArgumentException.

能帮您找出问题所在吗?

Can you please help in identifying the problem.

Web服务代码:

@Path("/employee")
public class EmployeeInfoService {

    // This method is called if TEXT_PLAIN is request
    @GET
    @Path("/get/{id}")
    @Produces(MediaType.APPLICATION_ATOM_XML)
    public Employee getEmplById(@PathParam("id") String id) {
        System.out.println("sayPlainTextHello");
        EmployeeDAO dbHandler = new EmployeeDAOImpl();
        Employee fetchedEmployee = dbHandler.findEmployee(Integer.parseInt(id));
        return fetchedEmployee;
    }

    @GET
    @Path("/get")
    @Produces(MediaType.APPLICATION_ATOM_XML)
    public Employee getEmplByGetReqParam(@QueryParam("param1") String id) {
        System.out.println("getEmplByGetReqParam");
        EmployeeDAO dbHandler = new EmployeeDAOImpl();
        Employee fetchedEmployee = dbHandler.findEmployee(Integer.parseInt(id));
        return fetchedEmployee;
    }

    @POST
    @Path("/get")
    @Produces("MediaType.TEXT_PLAIN")
    public String getEmplByPostReqParam(@FormParam("param1") String id) {
        System.out.println("getEmplByPostReqParam");
        EmployeeDAO dbHandler = new EmployeeDAOImpl();
        Employee fetchedEmployee = dbHandler.findEmployee(Integer.parseInt(id));
        return fetchedEmployee.toString();
    }

}

我使用了两个客户端来测试服务: 1. Google chrome的Advance Rest客户端

I used two client to test the service: 1. Google chrome's Advance Rest client

url - http://myhost:14443/de.vogella.jersey.first/rest/employee/get
payload - [param1: 1]
method type - [post]

  1. 核心Java代码

  1. core java code

   String url = "http://myhost:14443/de.vogella.jersey.first/rest/employee/get";
   String charset = "UTF-8";
   String param1 = "1";

    String query = String.format("param1=%s",
            URLEncoder.encode(param1, charset));

    URL service = new URL(url);
    URLConnection connection = service.openConnection();
    connection.setDoOutput(true); // Triggers POST.
    connection.setRequestProperty("Accept-Charset", charset);
    connection.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded;charset=" + charset);
    OutputStream output = connection.getOutputStream();
    try {
        output.write(query.getBytes(charset));
    } catch (Exception e) {
        // TODO: handle exception
    }

Stacktrace:

Stacktrace:

</pre></p><p><b>root cause</b> <pre>java.lang.IllegalArgumentException: java.text.ParseException: End of header
    com.sun.jersey.core.header.MediaTypes.createQualitySourceMediaTypes(MediaTypes.java:289)
    com.sun.jersey.core.header.MediaTypes.createQualitySourceMediaTypes(MediaTypes.java:274)
    com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller.addProduces(IntrospectionModeller.java:171)
    com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller.workOutSubResourceMethodsList(IntrospectionModeller.java:342)
    com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller.createResource(IntrospectionModeller.java:126)
    com.sun.jersey.server.impl.application.WebApplicationImpl.getAbstractResource(WebApplicationImpl.java:744)
    com.sun.jersey.server.impl.application.WebApplicationImpl.createAbstractResourceModelStructures(WebApplicationImpl.java:1564)
    com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1328)
    com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:168)
    com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:774)
    com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:770)
    com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:770)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:765)
    com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:489)
    com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:319)
    com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605)
    com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:374)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:557)
    javax.servlet.GenericServlet.init(GenericServlet.java:160)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.lang.Thread.run(Unknown Source)
</pre></p><p><b>root cause</b> <pre>java.text.ParseException: End of header
    com.sun.jersey.core.header.reader.HttpHeaderReaderImpl.getNextCharacter(HttpHeaderReaderImpl.java:168)
    com.sun.jersey.core.header.reader.HttpHeaderReaderImpl.next(HttpHeaderReaderImpl.java:129)
    com.sun.jersey.core.header.reader.HttpHeaderListAdapter.next(HttpHeaderListAdapter.java:111)
    com.sun.jersey.core.header.reader.HttpHeaderListAdapter.next(HttpHeaderListAdapter.java:98)
    com.sun.jersey.core.header.reader.HttpHeaderReader.nextSeparator(HttpHeaderReader.java:116)
    com.sun.jersey.core.header.QualitySourceMediaType.valueOf(QualitySourceMediaType.java:85)
    com.sun.jersey.core.header.reader.HttpHeaderReader$5.create(HttpHeaderReader.java:360)
    com.sun.jersey.core.header.reader.HttpHeaderReader$5.create(HttpHeaderReader.java:358)
    com.sun.jersey.core.header.reader.HttpHeaderReader.readList(HttpHeaderReader.java:481)
    com.sun.jersey.core.header.reader.HttpHeaderReader.readList(HttpHeaderReader.java:473)
    com.sun.jersey.core.header.reader.HttpHeaderReader.readAcceptableList(HttpHeaderReader.java:461)
    com.sun.jersey.core.header.reader.HttpHeaderReader.readQualitySourceMediaType(HttpHeaderReader.java:365)
    com.sun.jersey.core.header.reader.HttpHeaderReader.readQualitySourceMediaType(HttpHeaderReader.java:373)
    com.sun.jersey.core.header.MediaTypes.createQualitySourceMediaTypes(MediaTypes.java:287)
    com.sun.jersey.core.header.MediaTypes.createQualitySourceMediaTypes(MediaTypes.java:274)
    com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller.addProduces(IntrospectionModeller.java:171)
    com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller.workOutSubResourceMethodsList(IntrospectionModeller.java:342)
    com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller.createResource(IntrospectionModeller.java:126)
    com.sun.jersey.server.impl.application.WebApplicationImpl.getAbstractResource(WebApplicationImpl.java:744)
    com.sun.jersey.server.impl.application.WebApplicationImpl.createAbstractResourceModelStructures(WebApplicationImpl.java:1564)
    com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1328)
    com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:168)
    com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:774)
    com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:770)
    com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:770)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:765)
    com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:489)
    com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:319)
    com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605)
    com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:374)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:557)
    javax.servlet.GenericServlet.init(GenericServlet.java:160)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.lang.Thread.run(Unknown Source)

推荐答案

我通过更改getEmplByPostReqParam()来解决此问题,如下所示:

I resolved the issue by changing the getEmplByPostReqParam() as follows:

@POST
@Path("/get")
public Response getEmplByPostReqParam(@FormParam("param1") String id) {
    System.out.println("getEmplByPostReqParam");
    EmployeeDAO dbHandler = new EmployeeDAOImpl();
    Employee fetchedEmployee = dbHandler.findEmployee(Integer.parseInt(id));
    ResponseBuilder rb = new ResponseBuilderImpl();
    rb.type(MediaType.APPLICATION_ATOM_XML);
    rb.entity(fetchedEmployee);
    return rb.build();
}

这对我来说很好,如果他们的方法更好或者这种方法有问题,请发表评论.

This is working fine for me, if their is some better approach or their is something wrong with this approach, please post comment.

这篇关于java.lang.IllegalArgumentException:java.text.ParseException:标头结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆