[英]JAX-RS and CDI / @Provider and beans.xml
I made a simple project that makes use of JAX-RS (RESTfull service) 我做了一个使用JAX-RS(RESTfull服务)的简单项目
I have a JAX-RS (RESTfull service) webservice project that deploys to JBoss AS 6.1. 我有一个部署到JBoss AS 6.1的JAX-RS(RESTfull服务)Web服务项目。 resteasy integrated with JSON is provided by JBoss 6.1 AS by default.
默认情况下,JBoss 6.1 AS提供与JSON集成的resteasy。 I wanted to change the date format of the default JSON resource.
我想更改默认JSON资源的日期格式。
I got some help from the Internet and added a class that extends JacksonJsonProvider
: 我从互联网上获得了一些帮助,并添加了一个扩展
JacksonJsonProvider
的类:
@Provider
@Produces("application/json")
public class MyJacksonJsonProvider extends JacksonJsonProvider {
public static final String pattern = "YYYY-MM-dd'T'HH:mm:ss";
@Override
public void writeTo(Object value, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
ObjectMapper mapper = locateMapper(type, mediaType);
// Set customized date format
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
mapper.getSerializationConfig().setDateFormat(sdf);
super.writeTo(value, type, genericType, annotations, mediaType, httpHeaders, entityStream);
}
}
This works well until I added an empty beans.xml under WebContent/WEB-INF for CDI injection. 直到我在WebContent / WEB-INF下为CDI注入添加了一个空的beans.xml为止,这一直很好。
MyJacksonJsonProvider
doesn't get called and I still get the default JSON date format. MyJacksonJsonProvider
没有被调用,我仍然得到默认的JSON日期格式。
Even adding the following dependency under pom.xml did not help: 即使在pom.xml下添加以下依赖项也无济于事:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-cdi</artifactId>
<version>2.2.1.GA</version>
</dependency>
Does anyone have any idea why MyJacksonJsonProvider
is ignored if I have an empty beans.xml under "WebContent/WEB-INF" folder? 如果在“ WebContent / WEB-INF”文件夹下有空的beans.xml,是否有人知道为什么忽略
MyJacksonJsonProvider
? Thanks a lot in advance! 在此先多谢!
FYI, this is the sample model class: 仅供参考,这是示例模型类:
@XmlRootElement(name = "movie")
public class Movie {
String name;
String director;
int year;
Date date;
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public String getDirector() {
return director;
}
public void setDirector(String director) {
this.director = director;
}
@XmlAttribute
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
@XmlElement
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
And this is the class that produces JSON resource: 这是产生JSON资源的类:
@Path("/json/movie")
public class JSONService {
@GET
@Path("/get")
@Produces("application/json")
public Movie getMovieInJSON() {
Movie movie = new Movie();
movie.setName("Little flower");
movie.setDirector("Zhang Zheng");
movie.setYear(1979);
movie.setDate(new Date());
return movie;
}
}
I believe I've run into this as well. 我相信我也遇到过这个问题。 It's a known issue (RESTEASY-555) , and the work-around, if you can't upgrade Resteasy to >= 2.3.3.Final, is to tell Weld to exclude
MyJacksonJsonProvider
as below: 这是一个已知问题(RESTEASY-555) ,如果无法将Resteasy升级到> = 2.3.3,最终的解决方法是告诉Weld排除
MyJacksonJsonProvider
,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:weld="http://jboss.org/schema/weld/beans"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://docs.jboss.org/cdi/beans_1_0.xsd
http://jboss.org/schema/weld/beans http://jboss.org/schema/weld/beans_1_1.xsd">
<weld:scan>
<weld:exclude name="MyJacksonJsonProvider" />
</weld:scan>
</beans>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.