将对象发布到Java中的REST Web服务

时间:2018-08-29 18:46:52

标签: java rest web-services

我正在编写一个Java应用程序,我想在其中通过RESTful Web服务添加事件。当我尝试从客户端应用程序添加事件时,出现以下异常:

INFO: Error parsing HTTP request header
 Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException: Invalid character found in the HTTP protocol
at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:517)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:291)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Unknown Source)

但是当我从浏览器中调用相同的URL时,如果添加注释@GET可以正常工作,但是如果我将注释@PUT或@POST而不是@GET添加,我会得到405状态代码。我在网络上更改了这些注释服务方法。我想知道是什么原因引起的,因为它在浏览器中工作正常,但是在从客户端应用程序调用时会引发异常。

这是REST服务上的方法:

@POST
@Path("/add/{name}/{start}/{end}/{location}/{category}/{description}") 
@Produces(MediaType.APPLICATION_JSON)
public Response addEvent(@PathParam("name") String name,@PathParam("start") String startStr,@PathParam("end") String endStr,
    @PathParam("location") String location,@PathParam("category") String category,@PathParam("description") String description) {
    System.out.println(name);
    LocalDateTime start=service.parseLocalDateTime(startStr);
    LocalDateTime end=service.parseLocalDateTime(endStr);
    Event event=new Event(name,start,end,location,category,description,false);
    if(service.addEvent(event)) {
        return Response.status(200).build();
    }
    return Response.status(500).entity("Error in event adding.").build();
}

这是我的客户方法:

public void addEvent(Event event) {
    String baseUrl="http://localhost:8080/InfoEvent/api/events/";
    StringBuilder sb=new StringBuilder();
    sb.append(event.getName());
    sb.append("/");
    sb.append(event.getStart());
    sb.append("/");
    sb.append(event.getEnd());
    sb.append("/");
    sb.append(event.getLocation());
    sb.append("/");
    sb.append(event.getCategory());
    sb.append("/");
    sb.append(event.getDescription());
    String parameter=sb.toString();
    String stringURL=baseUrl+"add/"+parameter;
    System.out.println(stringURL);
    try {
        URL url = new URL(stringURL);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        OutputStream os = conn.getOutputStream();
        // os.write("");
        os.flush();
        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new RuntimeException("Failed-greskaa : HTTP error code : " + conn.getResponseCode());
        }
        os.close();
        conn.disconnect();
    } catch (Exception e) {
        e.printStackTrace();
    }
}   

我还尝试在client方法中更改批注,同时也在web service方法中更改批注,但这没有帮助。

1 个答案:

答案 0 :(得分:0)

我没有发表评论的声誉,

由于异常显示“解析HTTP标头时出错”和“ HTTP协议中的无效字符”,因此服务器似乎无法理解该请求。

这里的答案表明它可能与HTTPS vs HTTP有关。 java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens