如何发送Java RESTful Web服务的HTTP错误? [英] How to send a HTTP error for a Java RESTful web service?

查看:88
本文介绍了如何发送Java RESTful Web服务的HTTP错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了教程,现在我想从中抛出错误这个webservice就像HTTP错误代码 403 400

I have done this tutorial and now I want to throw an error from this webservice, like HTTP error code 403 or 400.

我该怎么办?我注意到我有一个类型为 HttpServletResponse 的界面,但我不知道如何使用它。我必须导入其他东西吗?

How can i do this? I noticed that i have an interface of type HttpServletResponse, but I don't know how I can use it. Do I have to import something else?

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;


推荐答案

你可以这样做。

You can do it like this.

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

这篇关于如何发送Java RESTful Web服务的HTTP错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆