Results 1 to 10 of 10

Thread: Automatically provide JSON data views for finders

  1. #1
    Join Date
    Oct 2009
    Location
    Munich, Germany
    Posts
    103

    Default Automatically provide JSON data views for finders

    What about automatically providing JSON data (views) for manually added finders or lists as an option? So one could use em for visualising the data e.g. with extjs grids or other Javascript frameworks.

    Is this an option, or is there actually another way to achieve this?

    Thanks Fireball

  2. #2
    Join Date
    Mar 2008
    Location
    Sydney, AU
    Posts
    974

    Default

    Spring 3 offers a new ContentNegotiatingViewResolver which you can register as follows in webmvc-config.xml:

    Code:
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
      <property name="mediaTypes">
        <map>
          <entry key="json" value="application/json"/>
        </map>
      </property>
      <property name="defaultViews">
        <list>
          <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
        </list>
      </property>
    </bean>
    And then register the Jackson dependencies in your project pom file:

    Code:
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>com.springsource.org.codehaus.jackson</artifactId>
        <version>1.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>com.springsource.org.codehaus.jackson.mapper</artifactId>
        <version>1.0.0</version>
    </dependency>
    This allows you to retrieve resources as json documents by appending .json to the list or show REST URIs (ie http://localhost:8080/petclinic/owner/1.json). The same can be done for XML (you could use XStream for example).

    In case you have cyclic references in your application you need to deal with them accordingly though.

    HTH,
    Stefan

  3. #3

    Default

    Cool, that's really helpful! :-)

    Couple of quick add-ons to that:

    How do I make some of the fields not visible?

    Are there similar POST/PUT/DELETE alternatives to this GET?

    Are there similar tricks I can do for invoking finders and getting a JSON result?

    Cheers

    Nik

  4. #4

    Default

    Just to follow up on the hiding:

    Fields prefixed with "ajc$interField" would be great candidates for hiding away. :-)

  5. #5
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    Andy Clement has been doing some work on ITD improvements re the "ajc" prefixes to ITD members.
    Ben Alex
    Project Founder, Spring UAA, Spring Roo and Spring Security

  6. #6
    Join Date
    Jan 2009
    Location
    Huntington Beach, CA
    Posts
    718

    Default

    So how do we deal with circular dependencies? Is there an annotation to use?

    Or can we just add a Controller method to our controller class (not .aj file) and do a special request mapping, then the finder or query that we run doesn't set the inverse/mappedby mapping so that you can only go one direction.

    Thanks

    Mark

  7. #7
    Join Date
    Feb 2010
    Posts
    23

    Default

    Quote Originally Posted by Stefan Schmidt View Post
    Spring 3 offers a new ContentNegotiatingViewResolver which you can register as follows in webmvc-config.xml:

    Code:
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
      <property name="mediaTypes">
        <map>
          <entry key="json" value="application/json"/>
        </map>
      </property>
      <property name="defaultViews">
        <list>
          <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
        </list>
      </property>
    </bean>
    And then register the Jackson dependencies in your project pom file:

    Code:
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>com.springsource.org.codehaus.jackson</artifactId>
        <version>1.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>com.springsource.org.codehaus.jackson.mapper</artifactId>
        <version>1.0.0</version>
    </dependency>
    This allows you to retrieve resources as json documents by appending .json to the list or show REST URIs (ie http://localhost:8080/petclinic/owner/1.json). The same can be done for XML (you could use XStream for example).

    In case you have cyclic references in your application you need to deal with them accordingly though.

    HTH,
    Stefan
    I tried the above and maven isn't able to find the necessary jars. Did something change lately? Are those dependencies still valid?

  8. #8
    Join Date
    Mar 2008
    Location
    Sydney, AU
    Posts
    974

    Default

    These dependencies should still be valid. Maybe you need to try again. To see the artifacts in the repo you can take a look here: https://s3browse.springsource.com/br....mapper/1.0.0/

    -Stefan
    Stefan Schmidt
    Software Engineer, Spring Roo
    SpringSource - a division of VMware
    twitter @schmidtstefan

  9. #9
    Join Date
    Jul 2010
    Posts
    119

    Default

    I'm getting the same thing, not being able to find it in the default repositories set up by Roo.
    Code:
    <repositories>
            <repository>
                <id>spring-maven-release</id>
                <name>Spring Maven Release Repository</name>
                <url>http://maven.springframework.org/release</url>
            </repository>
            <repository>
                <id>spring-maven-milestone</id>
                <name>Spring Maven Milestone Repository</name>
                <url>http://maven.springframework.org/milestone</url>
            </repository>
            <repository>
                <id>spring-roo-repository</id>
                <name>Spring Roo Repository</name>
                <url>http://spring-roo-repository.springsource.org/release</url>
            </repository>
    	<repository>
                <id>JBoss Repo</id>
                <url>https://repository.jboss.org/nexus/content/repositories/releases</url>
                <name>JBoss Repo</name>
            </repository>
        </repositories>
    Code:
    [Thread-6] Downloading: http://maven.springframework.org/release/org/codehaus/jackson/com.springsource.org.codehaus.jackson/1.0.0/com.springsource.org.codehaus.jackson-1.0.0.pom
    [Thread-6] [INFO] Unable to find resource 'org.codehaus.jackson:com.springsource
    .org.codehaus.jackson:pom:1.0.0' in repository spring-maven-release (http://maven.springframework.org/release)
    [Thread-6] Downloading: http://maven.springframework.org/milestone/org/codehaus/
    jackson/com.springsource.org.codehaus.jackson/1.0.0/com.springsource.org.codehaus.jackson-1.0.0.pom
    [Thread-6] [INFO] Unable to find resource 'org.codehaus.jackson:com.springsource
    .org.codehaus.jackson:pom:1.0.0' in repository spring-maven-milestone (http://maven.springframework.org/milestone)
    [Thread-6] Downloading: http://spring-roo-repository.springsource.org/release/org/codehaus/jackson/com.springsource.org.codehaus.jackson/1.0.0/com.springsource.
    org.codehaus.jackson-1.0.0.pom
    [Thread-6] [INFO] Unable to find resource 'org.codehaus.jackson:com.springsource
    .org.codehaus.jackson:pom:1.0.0' in repository spring-roo-repository (http://spring-roo-repository.springsource.org/release)
    [Thread-6] Downloading: https://repository.jboss.org/nexus/content/repositories/
    releases/org/codehaus/jackson/com.springsource.org.codehaus.jackson/1.0.0/com.springsource.org.codehaus.jackson-1.0.0.pom
    [Thread-6] [INFO] Unable to find resource 'org.codehaus.jackson:com.springsource
    .org.codehaus.jackson:pom:1.0.0' in repository JBoss Repo (https://repository.jboss.org/nexus/content/repositories/releases)
    [Thread-6] Downloading: http://repo1.maven.org/maven2/org/codehaus/jackson/com.s
    pringsource.org.codehaus.jackson/1.0.0/com.springsource.org.codehaus.jackson-1.0.0.pom
    [Thread-6] [INFO] Unable to find resource 'org.codehaus.jackson:com.springsource
    .org.codehaus.jackson:pom:1.0.0' in repository central (http://repo1.maven.org/maven2)

  10. #10
    Join Date
    Jul 2010
    Posts
    119

    Default

    Added this repository and it worked:

    Code:
        
    <repository>
          <id>springsource-external</id>
          <url>http://repository.springsource.com/maven/bundles/external</url>
    </repository>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •