301 Moved Permanently:
URI uri = ...
return Response.status(Status.MOVED_PERMANENTLY).location(uri).build();
303 See Other (aka POST/redirect/GET):
URI uri = ...
return Response.seeOther(uri).build();
307 Temporary Redirect:
URI uri = ...
return Response.temporaryRedirect(uri).build();
For more details, the Response class documentation may be useful.
Other details that can be useful when using JAX-RS
You also can inject the UriInfo in your REST endpoints:
@Context
UriInfo uriInfo;
And get some useful information, such as the base URI and the absolute path of the request. It will be useful when building the URI for redirection.
A resource method with redirection will be like:
@Path("/foo")
public class MyEndpoint {
@Context
private UriInfo uriInfo;
@POST
@Produces(MediaType.APPLICATION_JSON)
public Response myMethod() {
URI uri = uriInfo.getBaseUriBuilder().path("bar").build();
return Response.temporaryRedirect(uri).build();
}
}
Response.temporaryRedirect(uriInfo.getBaseUriBuilder().path("/").build()).build();这样是直接跳转到根路径下,需要注意的是这个方法的跳转方式GET,POST等会延用进入该方法时的方法,如果是POST方法进入的那么跳转后的方法还是post。
Response.seeOther(uriInfo.getBaseUriBuilder().path("/").build()).build();这样的跳转方式也是直接跳到根路径下,这种方式下的跳转采用的是GET方法
JSP中response.sendRedirect()与request.getRequestDispatcher().forward(request,response)这两个对象都可以使页面跳转,但是二者是有很大的区别的,分条来说,有以下几点:
①response.sendRedirect(url)-----重定向到指定URL
request.getRequestDispatcher(url).forward(request,response) -----请求转发到指定URL
②response.sendRedirect(url)-----是客户端跳转
request.getRequestDispatcher(url).forward(request,response) -----是服务器端跳转
③response.sendRedirect(url)跳转到指定的URL地址后,上个页面(跳转之前的原来页面)中的请求全部结束,原request对象将会消亡,数据将会消失。紧接着,当前新页面会新建request对象,即产生新的request对象。
【详细过程:redirect 会首先发一个response给浏览器,然后浏览器收到这个response后再发一个requeset给服务器,服务器接收后发新的response给浏览器。这时页面从浏览器获取来的是一个新的request。这时,在原来跳转之前的页面用request.setAttribute存的东西都没了,如果在当前的新页面中用request.getAttribute取,得到的将会是null。】
request.getRequestDispatcher(url).forward(request,response)是采用请求转发方式,在跳转页面的时候是带着原来页面的request和response跳转的,request对象始终存在,不会重新创建。
【详细过程:forward 发生在服务器内部, 是在浏览器完全不知情的情况下发给了浏览器另外一个页面的response. 这时页面收到的request不是从浏览器直接发来的,可能是在转页时己经用request.setAttribute在request里放了数据,在转到的页面就可以直接用request.getAttribute获得数据了。】
④使用response.sendRedirect()地址栏中的网址将改变
使用request.getRequestDispatcher().forward(request,response)地址栏中的网址保持不变。
⑤使用response.sendRedirect()时如果需要传递参数,那只能在url后加参数,如:url?id=1,而不能通过request或response方式。
使用request.getRequestDispatcher().forward(request,response)如果需要传递参数,可以在程序内通过response.setAttribute("name",name)来传至下一个页面.而不能在后面带参数传递,比如servlet?name=frank这样不行。
⑥运用sendRedirect()方法可以让你重定向到任何URL,而forward()方法只能重定向到同一个Web应用程序中的某个资源。
表单form中的action="/uu";sendRedirect("/uu");表示相对于服务器根路径。如服务器根路径是http://localhost:8080/Test则提交至http://localhost:8080/uu;而Forward代码中的"/uu"则代表相对于WEB应用的路径。如http://localhost:8080/Test应用则提交至http://localhost:8080/Test/uu。
⑦运用HttpServletResponse接口的sendRedirect()方法
sendRedirect()是在用户的浏览器端工作,同时它可以重定向至不同的主机上,sendRedirect()可以重定向有frame的jsp文件。
假设转发代码包含于注册的servlet-url为/ggg/tt;jsp为/ggg/tt.jsp。
绝对路径:response.sendRedirect("http://www.brainysoftware.com")发送至http://www.brainysoftware.com
根路径:response.sendRedirect("/ooo")发送至http://localhost:8080/ooo
相对路径:response.sendRedirect("ooo")发送至http://localhost:8080/Test/ggg/ooo。
sendRedirect等同于此方式:
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String newLocn = "/newpath/jsa.jsp";
response.setHeader("Location",newLocn);
⑧运用RequestDispatcher接口的Forward()方法
forward()无法重定向至有frame的jsp文件,可以重定向至有frame的html文件,
只有在客户端没有输出时才可以调用forward方法。如果当前页面的缓冲区(buffer)不是空的,那么你在调用forward方法前必须先清空缓冲区。
"/"代表相对与web应用路径
RequestDispatcher rd = request.getRequestDispatcher("/ooo");
rd.forward(request, response);提交至http://localhost:8080/Test/ooo
RequestDispatcher rd = getServletContext().getRequestDispatcher("/ooo");
rd.forward(request, response);提交至http://localhost:8080/Test/ooo
RequestDispatcher rd =getServletContext().getNamedDispatcher("TestServlet");(TestServlet为一个<servlet-name>)
rd.forward(request, response);提交至名为TestServlet的servlet
如果在<jsp:forward>之前有很多输出,前面的输出已使缓冲区满,将自动输出到客户端,那么该语句将不起作用,这一点应该特别注意。
另外要注意:它不能改变浏览器地址,刷新的话会导致重复提交。
从http://localhost:8080/Test/gw/page.jsp中转发
<jsp:forward page="OtherPage.jsp"/>在JSP页面被解析后转换成pageContext.forward("OtherPage.jsp");
"/OtherPage.jsp"提交到http://localhost:8080/Test/OtherPage.jsp
"OtherPage.jsp"提交到http://localhost:8080/Test/gw/OtherPage.jsp
此外,除了这两种页面跳转方法,还有一种比较方便的方法:
Meta Refresh方法
这种方法是由HTML提供的,Meta本身就是HTML标签。使用方法是:
<meta http-equiv="refresh" content="5; url=http://www.dreamdu.com/" />
相应的java代码是:
String content=stayTime+";URL="+URL;
response.setHeader("REFRESH",content);
响应码 | 含义 |
100 | 继续 |
101 | 分组交换协议 |
200 | OK |
201 | 被创建 |
202 | 被采纳 |
203 | 非授权信息 |
204 | 无内容 |
205 | 重置内容 |
206 | 部分内容 |
300 | 多选项 |
301 | 永久地传递 |
302 | 找到 |
303 | 参见其他 |
304 | 未改动 |
305 | 使用代理 |
307 | 暂时重定向 |
400 | 错误请求 |
401 | 未授权 |
402 | 要求付费 |
403 | 禁止 |
404 | 未找到 |
405 | 不允许的方法 |
406 | 不被采纳 |
407 | 要求代理授权 |
408 | 请求超时 |
409 | 冲突 |
410 | 过期的 |
411 | 要求的长度 |
412 | 前提不成立 |
413 | 请求实例太大 |
414 | 请求URL太大 |
415 | 不支持的媒体类型 |
416 | 无法满足的请求范围 |
417 | 失败的预期 |
500 | 内部错误 |
501 | 未被使用 |
502 | 网关错误 |
503 | 不可用的服务 |
504 | 网关超时 |
505 | HTTP版本未被支持 |
一、成功
从 200 到 399 为成功码,表示请求处理成功。
如果方法返回值不为null,则返回码是 200;如果返回值为 null 或者为 void,则返回码为 204,表示无内容。
二、错误
从 400 到 599 表示处理错误。
例如 404表示网页未找着;如果请求的期望的返回交换类型不对,则返回 406,表示不可接爱;如果请求的方法未找着,则返回 405,表示方法不允许,这个返回结果对于HEAD和OPTIONS请求方法例外,对于HEAD会试图去查找能处理相同URI的GET方法;对于OPTION,会返回一些自动生成的信息。
三、复杂的响应
对于不能简单处理的返回信息,则可以返回javax.ws.rs.core.Response对象:
- public abstract class Response {
- public abstract Object getEntity();
- public abstract int getStatus();
- public abstract MultivaluedMap<String, Object> getMetadata();
- ...
- }
Response对象不能直接创建,需要通过javax.ws.rs.core.Response.ResponseBuilder来创建:
- public abstract class Response {
- ...
- public static ResponseBuilder status(Status status) {...}
- public static ResponseBuilder status(int status) {...}
- public static ResponseBuilder ok() {...}
- public static ResponseBuilder ok(Object entity) {...}
- public static ResponseBuilder ok(Object entity, MediaType type) {...}
- public static ResponseBuilder ok(Object entity, String type) {...}
- public static ResponseBuilder ok(Object entity, Variant var) {...}
- public static ResponseBuilder serverError() {...}
- public static ResponseBuilder created(URI location) {...}
- public static ResponseBuilder noContent() {...}
- public static ResponseBuilder notModified() {...}
- public static ResponseBuilder notModified(EntityTag tag) {...}
- public static ResponseBuilder notModified(String tag) {...}
- public static ResponseBuilder seeOther(URI location) {...}
- public static ResponseBuilder temporaryRedirect(URI location) {...}
- public static ResponseBuilder notAcceptable(List<Variant> variants) {...}
- public static ResponseBuilder fromResponse(Response response) {...}
- ...
- }
ResponseBuilder是一个用来创建单个Response实例的工厂类, 首先将要创建的response对象的状态存起来,最后当状态设置完成了,就使用builder去初始化Response:
- public static abstract class ResponseBuilder {
- public abstract Response build();
- public abstract ResponseBuilder clone();
- public abstract ResponseBuilder status(int status);
- public ResponseBuilder status(Status status) {...}
- public abstract ResponseBuilder entity(Object entity);
- public abstract ResponseBuilder type(MediaType type);
- public abstract ResponseBuilder type(String type);
- public abstract ResponseBuilder variant(Variant variant);
- public abstract ResponseBuilder variants(List<Variant> variants);
- public abstract ResponseBuilder language(String language);
- public abstract ResponseBuilder language(Locale language);
- public abstract ResponseBuilder location(URI location);
- public abstract ResponseBuilder contentLocation(URI location);
- public abstract ResponseBuilder tag(EntityTag tag);
- public abstract ResponseBuilder tag(String tag);
- public abstract ResponseBuilder lastModified(Date lastModified);
- public abstract ResponseBuilder cacheControl(CacheControl cacheControl);
- public abstract ResponseBuilder expires(Date expires);
- public abstract ResponseBuilder header(String name, Object value);
- public abstract ResponseBuilder cookie(NewCookie... cookies);
- }
例如:
- @Path("/textbook")
- public class TextBookService {
- @GET
- @Path("/restfuljava")
- @Produces("text/plain")
- public Response getBook() {
- String book = ...;
- ResponseBuilder builder = Response.ok(book);
- builder.language("fr").header("Some-Header", "some value");
- return builder.build();
- }
- }
四、Cookie
JAX-RS使用了一个简单的类去表示一个cookie值,它就是javax.ws.rs.core.NewCookie:
- public class NewCookie extends Cookie {
- public static final int DEFAULT_MAX_AGE = −1;
- public NewCookie(String name, String value) {}
- public NewCookie(String name, String value, String path,
- String domain, String comment,
- int maxAge, boolean secure) {}
- public NewCookie(String name, String value, String path,
- String domain, int version, String comment,
- int maxAge, boolean secure) {}
- public NewCookie(Cookie cookie) {}
- public NewCookie(Cookie cookie, String comment,
- int maxAge, boolean secure) {}
- public static NewCookie valueOf(String value)
- throws IllegalArgumentException {}
- public String getComment() {}
- public int getMaxAge() {}
- public boolean isSecure() {}
- public Cookie toCookie() {}
- }
要返回Cookie,只需要传入它到Response中:
- @GET
- public Response get() {
- NewCookie cookie = new NewCookie("key", "value);
- ResponseBuilder builder = Response.ok("hello", "text/plain");
- return builder.cookies(cookie).build();
- }
五、状态类别
除了直接写数据外,JAX-RS定义了一个状态值的枚举类别:
- public enum Status {
- OK(200, "OK"),
- CREATED(201, "Created"),
- ACCEPTED(202, "Accepted"),
- NO_CONTENT(204, "No Content"),
- MOVED_PERMANENTLY(301, "Moved Permanently"),
- SEE_OTHER(303, "See Other"),
- NOT_MODIFIED(304, "Not Modified"),
- TEMPORARY_REDIRECT(307, "Temporary Redirect"),
- BAD_REQUEST(400, "Bad Request"),
- UNAUTHORIZED(401, "Unauthorized"),
- FORBIDDEN(403, "Forbidden"),
- NOT_FOUND(404, "Not Found"),
- NOT_ACCEPTABLE(406, "Not Acceptable"),
- CONFLICT(409, "Conflict"),
- GONE(410, "Gone"),
- PRECONDITION_FAILED(412, "Precondition Failed"),
- UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"),
- INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
- SERVICE_UNAVAILABLE(503, "Service Unavailable");
- public enum Family {
- INFORMATIONAL, SUCCESSFUL, REDIRECTION,
- CLIENT_ERROR, SERVER_ERROR, OTHER
- }
- public Family getFamily()
- public int getStatusCode()
- public static Status fromStatusCode(final int statusCode)
- }
每个Status的值都关联的到一个特定HTTP的返回值族,这个族由Status.Family标识。例如 100范围的值被认识是信息性的;200范围的是成功;300范围的也是成功,但是被重定向的;400是client错误;500是服务器错误。
Response.status()和ResponseBuilder.status()都接受一个Status值,例如:
- @DELETE
- Response delete() {
- ...
- return Response.status(Status.GONE).build();
- }
六、javax.ws.rs.core.GenericEntity
当处理Response的返回对象(entity)时,如果是一个类似于MessageBodyWriter这样的支持泛型的对象,则问题来了: isWriteable()方法需要有泛弄的信息。然后不幸的是java的泛型信息只存在编译时,不存在运行时,因此没有一个简单的方法可以得到在运行时得到泛型的信息,因此MessageBodyWriter不知道如何输出对象。
为了解决这个问题,JAX-RS提供了一个帮助类 javax.ws.rs.core.GenericEntity 。例如:
- @GET
- @Produces("application/xml")
- public Response getCustomerList() {
- List<Customer> list = new ArrayList<Customer>();
- list.add(new Customer(...));
- GenericEntity entity = new GenericEntity<List<Customer>>(list){};
- return Response.ok(entity);
- }
GenericEntity也是一个泛型模板,只需要将输出entity的泛型信息加到它上,并且把对象做为一个参数传入即可。
七、javax.ws.rs.WebApplicationException
WebApplicationException是一个内置、非检测异常,支持传入Response对象或者状态码:
- public class WebApplicationException extends RuntimeException {
- public WebApplicationException() {...}
- public WebApplicationException(Response response) {...}
- public WebApplicationException(int status) {...}
- public WebApplicationException(Response.Status status) {...}
- public WebApplicationException(Throwable cause) {...}
- public WebApplicationException(Throwable cause,
- Response response) {...}
- public WebApplicationException(Throwable cause, int status) {...}
- public WebApplicationException(Throwable cause,
- Response.Status status) {...}
- public Response getResponse() {...]
- }
当JAX-RS碰到一个WebApplicationException抛出时,它就捕获这个异常,调用它的getResponse()方法去获取Response,发回给client端。 如果应用以一个状态码或者Response初始化了WebApplicationException,则这个状态码或者Response将被用来创建真正的HTTP响应;或者,会直接返回 500, “Internal Server Error“给客户端,例如:
- @Path("/customers")
- public class CustomerResource {
- @GET
- @Path("{id}")
- @Produces("application/xml")
- public Customer getCustomer(@PathParam("id") int id) {
- Customer cust = findCustomer(id);
- if (cust == null) {
- throw new WebApplicationException(Response.Status.NOT_FOUND);
- }
- return cust;
- }
- }
这里如果没找着客户,会返回404错误
八、错误匹配
应用中可能有各种各样的,来自应用或者第三方包的异常,如果仅依赖于容器提供的错误处理方式,则可能灵活度不够: 捕获这些异常,然后包装到WebApplicationException中会让人觉得相当乏味。
这里的另外一种选择就是使用javax.ws.rs.ext.ExceptionMapper,这个对象知道怎么匹配一个抛出的异常到一个Repsonse对象上:
- public interface ExceptionMapper<E extends Throwable> {
- Response toResponse(E exception);
- }
例如对于JPA有EntityNotFoundException:
- @Provider
- public class EntityNotFoundMapper
- implements ExceptionMapper<EntityNotFoundException> {
- public Response toResponse(EntityNotFoundException e) {
- return Response.status(Response.Status.NOT_FOUND).build();
- }
- }
注: ExceptionMapper的实现,必须加上@Provider注释
ExceptionMapper也支持异常层级关系,例如A 继承 B,如果找不到A的mapper,则会向上查找B的mapper。
最后ExceptionMapper使用JAX-RS的deployment API进行注册,可以用Application.
ref:https://stackoverflow.com/questions/36638150/best-approach-to-redirect-an-url-using-rest
https://blog.csdn.net/qinzhenhua100/article/details/53227841