Note that there are some explanatory texts on larger screens.

plurals
  1. POREST webservice don't use my JsonSerializer
    primarykey
    data
    text
    <p>i implement an restful webservice with jersey 2.0 (jax-rs 2.0) and jackson on a glassfish 4 (i also try it in tomcat 8 with the same result). I have an dto with an date propertie, which i want send to the webservice.</p> <p>For serialization and deserialization i have my own (De)Serializer. In junit test i send the bean to the webservice and the webservice send the same bean back.</p> <p>So In the request i see that the date is formated like <code>yyyy-MM-dd'T'HH:mm:ss.SSSZ</code>, but in the response the date is like an unixtimestamp and my Deserializer throws an Exception <code>java.text.ParseException: Unparseable date: "1378980107682"</code></p> <p>Why my Server don't use my (De)Serializer classes?</p> <p>The Request:</p> <pre><code>1 &gt; Content-Type: application/json {"name":"name1","lastname":"lastname2","date":"2013-09-12T13:26:30.752+0200"} </code></pre> <p>The Response:</p> <p><code>2 &lt; X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition 4.0 Java/Oracle Corporation/1.7) {"name":"name1","lastname":"lastname2","date":1378985190752}</code></p> <p>My bean</p> <pre><code>@XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) @JsonAutoDetect @JsonIgnoreProperties (ignoreUnknown = true) @JsonInclude (Include.NON_NULL) public class User { private String name; private String lastname; private Date date; @JsonSerialize (using = MyDateSerializer.class) public Date getDate() { return date; } @JsonDeserialize (using = MyDateDeserializer.class) public void setDate(Date date) { this.date = date; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } } </code></pre> <p>My Serializer</p> <pre><code>public class MyDateSerializer extends JsonSerializer&lt;Date&gt; { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); @Override public void serialize(Date aDate, JsonGenerator aJsonGenerator, SerializerProvider aSerializerProvider) throws IOException, JsonProcessingException { System.out.println("MyDateSerializer.serialize()"); String dateString = dateFormat.format(aDate); System.out.println("dateString [" + dateString + "]"); aJsonGenerator.writeString(dateString); } } </code></pre> <p>My Deserializer</p> <pre><code>public class MyDateDeserializer extends JsonDeserializer&lt;Date&gt; { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); @Override public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { System.out.println("MyDateDeserializer.deserialize()"); String date = jsonParser.getText(); System.out.println("MyDateDeserializer.getText [" + date + "]"); try { return format.parse(date); } catch (ParseException e) { e.printStackTrace(); throw new RuntimeException(e); } } } </code></pre> <p>My Application</p> <pre><code>public class MyRESTExampleApplication extends ResourceConfig { public MyRESTExampleApplication() { packages("com.carano.fleet4all.restExample"); register(JacksonConfigurator.class); register(JacksonFeature.class); } } </code></pre> <p>My web.xml</p> <pre><code>&lt;display-name&gt;restExample&lt;/display-name&gt; &lt;servlet&gt; &lt;display-name&gt;JAX-RS REST Servlet&lt;/display-name&gt; &lt;servlet-name&gt;REST-Servlet&lt;/servlet-name&gt; &lt;servlet-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/servlet-class&gt; &lt;init-param&gt; &lt;param-name&gt;javax.ws.rs.Application&lt;/param-name&gt; &lt;param-value&gt;com.carano.fleet4all.restExample.MyRESTExampleApplication&lt;/param-value&gt; &lt;/init-param&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;REST-Servlet&lt;/servlet-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; </code></pre>
    singulars
    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.
    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