<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>
                                <td>
                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="https://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>
                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
Problem with StringConverter in JAX-RS
</h3>
<span style="margin-bottom: 10px;">
created by <a href="https://community.jboss.org/people/gstvie">Gerald Stockinger</a> in <i>Beginner's Corner</i> - <a href="https://community.jboss.org/message/830562#830562">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p>Hello,</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>I recently started working with JBoss again and I am struggling a bit.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>At the moment, I am trying to build a JAX-RS service that is returns a simple data structure. </p><p>One of the fields however has to be passed to a StringConverter.</p><p>The REST - service returns the json - data, however the String conversion is never executed.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>My environment ist jboss-eap-6.1. and I am using the jboss-javaee6-multi project structure. I attached the complete source to this post.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>I would be really, really thankful, if someone could help me with this problem :-)</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>My domain - model consists of two classes: MyObjectContainer and MyObject. MyObject should use a StringConverter, when it is marshalled to JSON - this is the following code:</p><p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p></p><pre class="jive-pre"><code class="jive-code jive-java">@Provider
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> MyConverter <font color="navy"><b>implements</b></font> StringConverter<MyObject><font color="navy">{</font>
 
    @Override
    <font color="navy"><b>public</b></font> MyObject fromString(String arg0) <font color="navy">{</font>
        MyObject my = <font color="navy"><b>new</b></font> MyObject();
        my.setLastName((arg0.split(<font color="red">", "</font>))[0]);
        my.setFirstName((arg0.split(<font color="red">", "</font>))[1]);
        <font color="navy"><b>return</b></font> my;
    <font color="navy">}</font>
 
    @Override
    <font color="navy"><b>public</b></font> String toString(MyObject arg0) <font color="navy">{</font>
        <font color="navy"><b>return</b></font> arg0.getLastName() + <font color="red">", "</font> + arg0.getFirstName();
    <font color="navy">}</font>
 
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>My Service looks like this and just returns some dummy information for testing: </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">@Path(<font color="red">"/context"</font>)
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> ContextResource <font color="navy">{</font>
    <font color="navy"><b>private</b></font> <font color="navy"><b>final</b></font> <font color="navy"><b>static</b></font> String MEDIA_TYPE = <font color="red">"application/vnd.mtp.context.v1+json"</font>;
       
    @GET
    @Produces(MEDIA_TYPE)
    <font color="navy"><b>public</b></font> MyObjectContainer getContext() <font color="navy">{</font>
        MyObjectContainer mycont = <font color="navy"><b>new</b></font> MyObjectContainer();
        MyObject my = <font color="navy"><b>new</b></font> MyObject();
        mycont.setId(1L);
        my.setLastName(<font color="red">"Doe"</font>);
        my.setFirstName(<font color="red">"John"</font>);
        mycont.setMy(my);
        <font color="navy"><b>return</b></font> mycont;
    <font color="navy">}</font>
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>But somehow I always get this Json String: </p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy">{</font><font color="red">"my"</font>:<font color="navy">{</font><font color="red">"firstName"</font>:<font color="red">"John"</font>,<font color="red">"lastName"</font>:<font color="red">"Doe"</font><font color="navy">}</font>,<font color="red">"id"</font>:1,<font color="red">"uri"</font>:<font color="red">"/mycontainer/1"</font><font color="navy">}</font>
</code></pre><p>instead of </p><pre class="jive-pre"><code class="jive-code">{"my": "Doe, John","id":1,"uri":"/mycontainer/1"}
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>the two domain objects look like this: </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">@JsonSerialize(include = Inclusion.NON_EMPTY)
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> MyObjectContainer <font color="navy"><b>implements</b></font> Serializable<font color="navy">{</font>
    <font color="navy"><b>public</b></font> <font color="navy"><b>static</b></font> <font color="navy"><b>final</b></font> String URIPATTERN =<font color="red">"/mycontainer/{id}"</font>;
       
    <font color="navy"><b>private</b></font> MyObject my;
 
    <font color="navy"><b>public</b></font> MyObject getMy() <font color="navy">{</font>
        <font color="navy"><b>return</b></font> my;
    <font color="navy">}</font>
...
<font color="navy">}</font>
 
 
 
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> MyObject <font color="navy">{</font>
    <font color="navy"><b>private</b></font> String firstName;
    <font color="navy"><b>private</b></font> String lastName;
    <font color="navy"><b>public</b></font> String getFirstName() <font color="navy">{</font>
        <font color="navy"><b>return</b></font> firstName;
    <font color="navy">}</font>
    <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> setFirstName(String firstName) <font color="navy">{</font>
        this.firstName = firstName;
    <font color="navy">}</font>
    <font color="navy"><b>public</b></font> String getLastName() <font color="navy">{</font>
        <font color="navy"><b>return</b></font> lastName;
    <font color="navy">}</font>
    <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> setLastName(String lastName) <font color="navy">{</font>
        this.lastName = lastName;
    <font color="navy">}</font>  
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>I tried quite a few things with web.xml (i.e. resteasy.scan, resteasy.providers,...), however I think it should be enough to include an empty web.xml and use a JaxRsActivactor class instead (as in the ticketmonster-tutorial).</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>To be complete - the relevant part of my pom.xml: </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code"><?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <artifactId>jboss-javaee6-multi</artifactId>
      <groupId>org.jboss.tools.example</groupId>
      <version>0.0.1-SNAPSHOT</version>
   </parent>
   <artifactId>jboss-javaee6-multi-web</artifactId>
   <packaging>war</packaging>
   <name>jboss-javaee6-multi: WAR Module</name>
   <url>http://jboss.org/jbossas</url>
   <licenses>
      <license>
         <name>Apache License, Version 2.0</name>
         <distribution>repo</distribution>
         <url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
      </license>
   </licenses>
   <dependencies>
      <!-- Dependency on the EJB module so we can use it's services if needed -->
      <dependency>
         <groupId>org.jboss.tools.example</groupId>
         <artifactId>jboss-javaee6-multi-ejb</artifactId>
         <type>ejb</type>
         <scope>provided</scope>
      </dependency>
      <!-- Import the JAX-RS API, we use provided scope as the API is included in JBoss AS 7 -->
      <dependency>
         <groupId>org.jboss.spec.javax.ws.rs</groupId>
         <artifactId>jboss-jaxrs-api_1.1_spec</artifactId>
         <scope>provided</scope>
      </dependency>
      <!-- Import the CDI API, we use provided scope as the API is included in JBoss AS 7 -->
      <dependency>
         <groupId>javax.enterprise</groupId>
         <artifactId>cdi-api</artifactId>
         <scope>provided</scope>
      </dependency>
     
      <!-- Import the JSF API, we use provided scope as the API is included in JBoss AS 7 -->
      <dependency>
         <groupId>org.jboss.spec.javax.faces</groupId>
         <artifactId>jboss-jsf-api_2.1_spec</artifactId>
         <scope>provided</scope>
      </dependency>
      <!-- Import the JPA API, we use provided scope as the API is included in JBoss AS 7 -->
      <dependency>
         <groupId>org.hibernate.javax.persistence</groupId>
         <artifactId>hibernate-jpa-2.0-api</artifactId>
         <scope>provided</scope>
      </dependency>
       <!-- JSR-303 (Bean Validation) Implementation -->
       <!-- Provides portable constraints such as @Email -->
       <!-- Hibernate Validator is shipped in JBoss AS 7 -->
       <dependency>
           <groupId>org.hibernate</groupId>
           <artifactId>hibernate-validator</artifactId>
           <scope>provided</scope>
           <exclusions>
               <exclusion>
                   <groupId>org.slf4j</groupId>
                   <artifactId>slf4j-api</artifactId>
               </exclusion>
           </exclusions>
       </dependency>
       <dependency>
           <groupId>org.jboss.resteasy</groupId>
           <artifactId>resteasy-jaxrs</artifactId>
           <version>2.3.7.Final</version>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.jboss.resteasy</groupId>
           <artifactId>resteasy-jackson-provider</artifactId>
           <version>2.3.6.Final</version>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.jboss.resteasy</groupId>
           <artifactId>resteasy-cdi</artifactId>
           <version>2.3.7.Final</version>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.codehaus.jackson</groupId>
           <artifactId>jackson-jaxrs</artifactId>
           <version>1.9.9</version>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.codehaus.jackson</groupId>
           <artifactId>jackson-mapper-asl</artifactId>
           <version>1.9.9</version>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.codehaus.jackson</groupId>
           <artifactId>jackson-core-asl</artifactId>
           <version>1.9.9</version>
           <scope>provided</scope>
       </dependency>
   </dependencies>
   <build>
      <finalName>${project.artifactId}</finalName>
      <plugins>
         <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>${version.war.plugin}</version>
            <configuration>
               <!-- Java EE 6 doesn't require web.xml, Maven needs to catch up! -->
               <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>If somebody could point me in the right direction, I would be forever thankful ;-).</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>BR,</p><p>Gerald </p></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Reply to this message by <a href="https://community.jboss.org/message/830562#830562">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in Beginner's Corner at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2075">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>