将发布请求发送到宁静的Web服务时遇到IllegalArgumentException
如果我注释后请求(getEmplByPostReqParam())的方法,则代码对于get请求可以正常工作,但使用getEmplByPostReqParam()时,所有请求均抛出IllegalArgumentException

您能帮忙找出问题所在吗?

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客户端

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



核心Java代码

   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
    }



堆栈跟踪:

</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()来解决此问题,如下所示:

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


这对我来说很好,如果有更好的方法或该方法有问题,请发表评论。

09-28 14:55