我有很简单的JAX-RS WS:
@Stateless
@Path("/foo")
public class FooFacadeBean {
@GET
@Produces(MediaType.TEXT_HTML)
public String performFooCall(@Context HttpServletRequest request, @Context HttpServletResponse response) {
response.setStatus(500);
return "Hello";
}
}
部署后,我执行:卷曲-v本地主机:8080 / foo,那么结果是:
About to connect() to localhost port 8080
Trying 127.0.0.1...
connected
Connected to localhost (127.0.0.1) port 8080
GET /foo HTTP/1.1
User-Agent: curl/7.26.0
Host: localhost:8080
Accept: */
HTTP/1.1 200 OK
X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.2 Java/Sun Microsystems Inc./1.6)
Server: GlassFish Server Open Source Edition 3.1.2
Content-Type: text/html
Transfer-Encoding: chunked
Date: Thu, 26 Jul 2012 13:52:10 GMT
Hello
Connection to host localhost left intact
Closing connection
正如你所看到的HTTP状态代码没有改变,尽管它是手动设置。 为什么会出现这种情况? 我花了很多的时间没有任何结果google搜索。
这样的解决方法,因为返回Response对象,像这样:
Response.status(404).entity("Hello").build();
完美的作品!
GlassFish使用JERSEY作为JAX-RS实现。 我用的Glassfish的嵌入式类型。