繁体   English   中英

如何为Java RESTful Web服务发送HTTP错误?

[英]How to send a HTTP error for a Java RESTful web service?

我已完成本教程 ,现在我想从此Web服务中抛出错误,如HTTP错误代码403400

我怎样才能做到这一点? 我注意到我有一个HttpServletResponse类型的接口,但我不知道如何使用它。 我还需要输入别的东西吗?

import java.util.Date;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.xml.ws.http.HTTPException;

import sun.awt.RequestFocusController;

import com.sun.jersey.spi.resource.Singleton;
import com.sun.research.ws.wadl.Request;
import com.sun.research.ws.wadl.Response;

你可以这样做。

@GET
public Response check(@QueryParam("username") String username) {
   if (facade.checkUser(username)) {
      return Response.status(Response.Status.NOT_FOUND).build();
   }
   return Response.ok().build();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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