简体   繁体   中英

Posting object to REST web service in Java

I am writing a java application where I want to add events through a RESTful web service. When I try adding an event from my client application I get the following exception:

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)

But when I call that same URL from the browser it works if I add annotation @GET, but if I put annotation @PUT, or @POST instead of @GET I get 405 status code.I changed these annotations in the web service method. I was wondering what might be causing the issue, since it works fine from the browser but throws the exception when called from the client app.

Here is the method on the REST service:

@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();
}

Here is my client method:

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

I also tried changing the annotations in the client method, while also changing it in the web service method, but it didn't help.

I don't have the reputation to comment,

Since the exception says 'Error parsing HTTP header' and 'invalid character in HTTP protocol', it looks like the server did not understand the request.

Answers in here suggest it may have to do with HTTPS vs HTTP. java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM