Announcement Announcement Module
Collapse
No announcement yet.
Automatically provide JSON data views for finders Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


    • #3
      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

      Comment


      • #4
        Just to follow up on the hiding:

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

        Comment


        • #5
          Andy Clement has been doing some work on ITD improvements re the "ajc" prefixes to ITD members.

          Comment


          • #6
            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

            Comment


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

              Comment


              • #8
                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

                Comment


                • #9
                  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)

                  Comment


                  • #10
                    Added this repository and it worked:

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

                    Comment

                    Working...
                    X