我正在使用托管在Google App Engine上的Java,Jetty和Jersey 2.18(目前最新)。
假设我有一项服务
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{userId}")
public Response getUser(@PathParam("userId") String userId)
{
...
}
当我做:
return Response.ok()
.entity(user)
.build();
我正确地收到了一个application / json内容类型和主体。但是当我这样做时:
return Response
.status(404)
.entity(new ResponseModel(100, "user not found"))
.build();
与返回任何4XX或5XX状态相同,我收到一个text / html内容类型以及以下HTML正文:
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>404 Not Found</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Not Found</h1>
</body>
</html>
而不是我放入.entity()的对象
编辑:这是我的web.xml
<?xml version="1.0" encoding="utf-8"?>
<web-app
version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.mypackage.services;org.codehaus.jackson.jaxrs</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>
org.glassfish.jersey.server.gae.GaeFeature;
org.glassfish.jersey.server.mvc.jsp.JspMvcFeature;
org.glassfish.jersey.media.multipart.MultiPartFeature;
</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.Trace</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattytern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>ObjectifyFilter</filter-name>
<filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ObjectifyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring Security Filter -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml </param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>GlobalResponseFilter</filter-name>
<filter-class>com.mypackage.GlobalResponseFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>GlobalResponseFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<security-constraint>
<web-resource-collection>
<web-resource-name>everything</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<!-- ** -->
<!-- ** General session timeout in minutes -->
<!-- ** -->
<session-config>
<session-timeout>1440</session-timeout>
</session-config>
</web-app>
ResponseModel只是一个基本的可序列化的Java类:
import java.io.Serializable;
public class ResponseModel implements Serializable
{
private static final long serialVersionUID = 1L;
private int code;
private Serializable data;
public ResponseModel()
{
}
public ResponseModel(int code, Serializable data)
{
System.err.println("Code " + code + " : " + data);
this.code = code;
this.data = data;
}
public int getCode()
{
return code;
}
public void setCode(int code)
{
this.code = code;
}
public Serializable getData()
{
return data;
}
public void setData(Serializable data)
{
this.data = data;
}
}
您可以使用以下配置再试一次:
<servlet>
<servlet-name>API</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
...
<init-param>
<param-name>jersey.config.server.response.setStatusOverSendError</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
此标志定义在发送4xx或5xx响应状态时,Jersey是否使用ServletResponse.sendError
(标志为false
)还是ServletResponse.setStatus
(标志为true
)。
调用ServletResponse.sendError
通常会重置响应实体和标头,并返回状态文本的(text / html)错误页面。
由于您要返回自己的自定义错误实体,因此需要将此标志设置为true
。
本文向大家介绍symfony2 返回404响应,包括了symfony2 返回404响应的使用技巧和注意事项,需要的朋友参考一下 示例 当在服务器上找不到资源时,将返回404响应。在Symfony中,可以通过引发NotFoundHttpException异常来创建此状态。为了避免use在控制器内部添加多余的语句,请使用类createNotFoundException()提供的Controller
前段时间,我设置了一个带有一些子域的站点(,...) 为什么辅助DNS没有给出子域的答案?它给出了像“test.example.com”这样的旧子域的答案。我必须如何解释SOA响应?什么意思?
我将Tomcat8.5与Eclipse集成在一起,但当我启动服务器并转到http://localhost:8080或http://localhost:8080/psite(配置了带有index.html的Eclipse项目)时,每次都会出现404错误。 我尝试“切换位置”-不起作用。 “服务器概述”屏幕中的“服务器配置”部分在我的Eclipse(霓虹灯)上丢失。 在窗口->首选项->运行时环境下:
[信息]将war部署到http://localhost:8080/app-ws上载:http://localhost:8080/manager/text/deploy?path=%2fapp-ws&update=true上载:http://localhost:8080/manager/text/deploy?path=%2fapp-ws&update=true(11702 KB,17334.8 K
我有以下配置。 如果我添加另一个
文档有点混乱。如果超时,该方法将返回什么?文件上写着“计算结果”,但如果计算超时怎么办?它是空的吗? 谢谢