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

问题描述 投票:1回答:1

我正在编写一个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状态代码。我在Web服务方法中更改了这些注释。我想知道是什么原因导致了这个问题,因为它可以从浏览器中正常工作,但是当从客户端应用程序调用时会抛出异常。

以下是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();
    }
}   

我也尝试更改客户端方法中的注释,同时也在Web服务方法中更改它,但它没有帮助。

java rest web-services
1个回答
0
投票

我没有评论的声誉,

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

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

© www.soinside.com 2019 - 2024. All rights reserved.