[jboss-user] [Beginner's Corner] - Problem with StringConverter in JAX-RS

Gerald Stockinger do-not-reply at jboss.com
Tue Jul 30 11:47:44 EDT 2013


Gerald Stockinger [https://community.jboss.org/people/gstvie] created the discussion

"Problem with StringConverter in JAX-RS"

To view the discussion, visit: https://community.jboss.org/message/830562#830562

--------------------------------------------------------------
Hello,

I recently started working with JBoss again and I am struggling a bit.

At the moment, I am trying to build a JAX-RS service that is returns a simple data structure. 
One of the fields however has to be passed to a StringConverter.
The REST - service returns the json - data, however the String conversion is never executed.

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.

I would be really, really thankful, if someone could help me with this problem :-)

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:

@Provider
public class MyConverter implements StringConverter<MyObject>{
 
    @Override
    public MyObject fromString(String arg0) {
        MyObject my = new MyObject();
        my.setLastName((arg0.split(", "))[0]);
        my.setFirstName((arg0.split(", "))[1]);
        return my;
    }
 
    @Override
    public String toString(MyObject arg0) {
        return arg0.getLastName() + ", " + arg0.getFirstName();
    }
 
}


My Service looks like this and just returns some dummy information for testing: 

@Path("/context")
public class ContextResource {
    private final static String MEDIA_TYPE = "application/vnd.mtp.context.v1+json";
        
    @GET
    @Produces(MEDIA_TYPE)
    public MyObjectContainer getContext() {
        MyObjectContainer mycont = new MyObjectContainer();
        MyObject my = new MyObject();
        mycont.setId(1L);
        my.setLastName("Doe");
        my.setFirstName("John");
        mycont.setMy(my);
        return mycont;
    }
}


But somehow I always get this Json String: 
{"my":{"firstName":"John","lastName":"Doe"},"id":1,"uri":"/mycontainer/1"}

instead of 
{"my": "Doe, John","id":1,"uri":"/mycontainer/1"}



the two domain objects look like this: 

@JsonSerialize(include = Inclusion.NON_EMPTY)
public class MyObjectContainer implements Serializable{
    public static final String URIPATTERN ="/mycontainer/{id}";
        
    private MyObject my;
 
    public MyObject getMy() {
        return my;
    }
 ...
}
 
 
 
public class MyObject {
    private String firstName;
    private String lastName;
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }   
}


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).

To be complete - the relevant part of my pom.xml: 


<?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>




If somebody could point me in the right direction, I would be forever thankful ;-).

BR,
Gerald
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/830562#830562]

Start a new discussion in Beginner's Corner at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2075]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20130730/37485359/attachment-0001.html 


More information about the jboss-user mailing list