如何在宁静的java服务中满足无效返回警告
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您确实不应该使用 GET 操作。
我将使用 POST 操作并返回 201 Created ,其中包含指向描述系统新状态的资源的链接。
例如,我将重定向到关闭状态资源:
客户端可以轮询资源以检查状态。
这个新的 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:
The the client can poll the resource to check on the status
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.
警告是正确的。 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.
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
尝试将其定义为 *V*oid 并返回 null。
Try to define it as *V*oid and return null.