如何在宁静的java服务中满足无效返回警告

发布于 2024-11-18 01:16:21 字数 568 浏览 3 评论 0原文

Java 代码:

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

Java 警告被抛出到控制台:

警告:HTTP GET 方法,public void com.myPackage.stop(java.lang.String,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)抛出 javax.servlet.ServletException、java.io.IOException,必须返回非 void 类型。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

街角卖回忆 2024-11-25 01:16:21

您确实不应该使用 GET 操作。

我将使用 POST 操作并返回 201 Created ,其中包含指向描述系统新状态的资源的链接。

例如,我将重定向到关闭状态资源:

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

客户端可以轮询资源以检查状态。

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

这个新的 GET 操作将始终返回该 id 的服务器状态。这样您就知道它是否正确关闭。如果服务器关闭需要很长时间,客户端可以轮询该资源以检查不同的关闭状态,直到完成。这样你就可以从原来的服务器关闭请求中快速返回。

如果您的进程中抛出异常,我不会将其返回给客户端,我会在服务器状态资源中获得异常的状态。当我可以将客户端表示为资源时,我总是避免让客户端处理异常情况。这允许您自由更改资源,例如当更改或修复导致异常的方法时,无需更改外部 API。

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.

愛上了 2024-11-25 01:16:21

警告是正确的。 GET 操作应该是幂等的,因此不应影响服务器状态。如果您没有返回任何内容,则该方法不能做任何有用的事情。您应该将其更改为 POST 或其他一些适当的 HTTP 操作。

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.

浅笑轻吟梦一曲 2024-11-25 01:16:21

url 必须如下。
{服务器名称}:{端口}/{应用程序名称}/{标准名称}/{您的服务名称}/{您的操作名称}

例如:http://localhost:8080/restful/jersy/user/service/getUserDets

休息 - 应用程序名称
球衣 - 标准名称{可选}
用户/服务 - 服务名称
getuserdets - 操作名称

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

疯了 2024-11-25 01:16:21

尝试将其定义为 *V*oid 并返回 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;
  }

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;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文