我遇到了春季3.1和泽西岛的问题 我想在我的REST服务上生成JSON,但我得到正常的HTML错误
这是我的ExceptionMapper
@Provider
public class MyExceptionMapper implements ExceptionMapper<ResourceNotFoundException>{
@Override
public Response toResponse(ResourceNotFoundException ex) {
return Response.status(404).entity(ex.getMessage()).type(MediaType.APPLICATION_JSON_TYPE).build();
}
}
并在我的Rest方法中抛出ResourceNotFoundException
@GET
@Path("/{uid}")
@Produces("application/json")
public Response getUser(@PathParam("uid") Long userId) {
User user = userService.getUserById(userId);
if(user==null) throw new ResourceNotFroundException("There is no user with id "+userId);
return Response.status(Status.OK).entity(mapper.map(user, UserDTO.class)).build();
}
这是我的应用程序上下文
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="xxx" />
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/home/resources/**" location="/resources/" />
<import resource="classpath*:bpm-applicationContext.xml" />
<import resource="classpath*:dao-applicationContext.xml" />
<import resource="classpath*:monitor-applicationContext.xml" />
<import resource="classpath*:logic-applicationContext.xml" />
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="home/**" />
<bean id="webContentInterceptor"
class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<property name="cacheSeconds" value="0" />
<property name="useExpiresHeader" value="true" />
<property name="useCacheControlHeader" value="true" />
<property name="useCacheControlNoStore" value="true" />
</bean>
</mvc:interceptor>
</mvc:interceptors>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean id="methodHandlerExceptionResolver" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
<property name="messageConverters">
<list>
<ref bean="stringHttpMessageConverter"/>
<ref bean="jsonConverter"/>
</list>
</property>
</bean>
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
</bean>
<!-- Dozer mapping for Entity <-> DTO mappings -->
<bean id="org.dozer.Mapper" class="org.dozer.DozerBeanMapper">
<property name="mappingFiles">
<list>
<value>dto-mappings.xml</value>
</list>
</property>
</bean>
</beans>
和我的web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Mist</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>JerseyREST</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.comarch.mist.ws.rest.v1</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.comarch.mist.ws.rest.v1.auth.AuthenticationFilter</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JerseyREST</servlet-name>
<url-pattern>/rest/1/*</url-pattern>
</servlet-mapping>
</web-app>
正如你所看到的,我也是普通的html servlet。 我想只在我的REST Servlet中处理JSON格式的错误。因此,来自/ rest / v1 / **的每个错误请求都应该产生JSON异常,并且对其他URL的请求应该返回HTML格式的正常异常。
修改
当我返回HTTP 200时,用户数据是JSON格式