Note that there are some explanatory texts on larger screens.

plurals
  1. POJAX-RS and CDI / @Provider and beans.xml
    primarykey
    data
    text
    <p>I made a simple project that makes use of JAX-RS (RESTfull service)</p> <p>I have a JAX-RS (RESTfull service) webservice project that deploys to JBoss AS 6.1. resteasy integrated with JSON is provided by JBoss 6.1 AS by default. I wanted to change the date format of the default JSON resource.</p> <p>I got some help from the Internet and added a class that extends <code>JacksonJsonProvider</code>:</p> <ul> <li><a href="https://stackoverflow.com/questions/8498413/accessing-jackson-object-mapper-in-resteasy">Accessing Jackson Object Mapper in RestEasy</a></li> <li><a href="https://stackoverflow.com/questions/4428109/jersey-jackson-json-date-serialization-format-problem-how-to-change-the-form">Jersey + Jackson JSON date format serialization - how to change the format or use custom JacksonJsonProvider</a></li> </ul> <pre class="lang-java prettyprint-override"><code>@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&lt;?&gt; type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap&lt;String,Object&gt; 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); } } </code></pre> <p>This works well until I added an empty beans.xml under WebContent/WEB-INF for CDI injection.</p> <p><code>MyJacksonJsonProvider</code> doesn't get called and I still get the default JSON date format.</p> <p>Even adding the following dependency under pom.xml did not help:</p> <pre class="lang-xml prettyprint-override"><code>&lt;dependency&gt; &lt;groupId&gt;org.jboss.resteasy&lt;/groupId&gt; &lt;artifactId&gt;resteasy-jaxrs&lt;/artifactId&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.jboss.resteasy&lt;/groupId&gt; &lt;artifactId&gt;resteasy-cdi&lt;/artifactId&gt; &lt;version&gt;2.2.1.GA&lt;/version&gt; &lt;/dependency&gt; </code></pre> <p>Does anyone have any idea why <code>MyJacksonJsonProvider</code> is ignored if I have an empty beans.xml under "WebContent/WEB-INF" folder? Thanks a lot in advance!</p> <p>FYI, this is the sample model class:</p> <pre class="lang-java prettyprint-override"><code>@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; } } </code></pre> <p>And this is the class that produces JSON resource:</p> <pre class="lang-java prettyprint-override"><code>@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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload