简体   繁体   中英

How do I satisfy void return warning in a restful java service

Java Code:

      @GET
      @Path("/stop/{id}")
      public void stop(
      @PathParam("id") String id,
      @Context HttpServletRequest request,
      @Context HttpServletResponse response) throws ServletException,
      IOException
      {
        server.stop(id);
      }

Java Warning's being thrown to console:

WARNING: A HTTP GET method, public void com.myPackage.stop(java.lang.String,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws javax.servlet.ServletException,java.io.IOException, MUST return a non-void type.

You really shouldn't be using a GET operation.

I would use a POST operation and return a 201 Created with a link to a resource which describes the new state of the system.

For example, I would redirect to a shutdown status resource:

POST /stop/123
...
201 CREATED
Location:
  http://acme.com/shutdownstatus/123

The the client can poll the resource to check on the status

GET /shutdownstatus/123
...
<shutdown xmlns="http://schemas.serverapp.com/shutdown">
  <status>pending</status>
  <message>Looks good so far</message>
</shutdown> 

This new GET operation will always return the state of the server for that id. That way you know if it was shutdown correctly or not. If server shutdown takes a long time, the client could poll that resource to check on different shutdown statuses until it is complete. This way you can return from the original server shutdown request quickly.

If an exception was thrown in your process, I wouldn't return it to the client, I would have a status for the exception in the server status resource. I always avoid making a client handle an exceptional case when I can represent it as a resource. This allows you to change the resource freely, such as when the exception causing method is changed or fixed, without changing the external API.

The warning is correct. A GET operation should be idempotent, so shouldn't affect server state. If you're not returning anything, the method can't be doing anything useful. You should change it to POST or some other appropriate HTTP operation.

url must be as follows. {servername}:{port}/{applicationname}/{standardname}/{your service name}/{your operation name}

ex: http://localhost:8080/restful/jersy/user/service/getUserDets

restful - applicationname
jersy - standardname {optional}
user/service - service name
getuserdets - operation name

Try to define it as * V *oid and return null.

  @GET
  @Path("/stop/{id}")
  public void stop(
  @PathParam("id") String id,
  @Context HttpServletRequest request,
  @Context HttpServletResponse response) throws ServletException,
  IOException
  {
    server.stop(id);
    return null;
  }

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