Skip to content
Browse files

Add basic JSON support for Example resource.

  • Loading branch information...
1 parent 90c3393 commit 1c6ca142e17182e950114f9eeeb4e49dc5603034 @skuenzli committed Nov 10, 2012
View
5 sample-webapp/src/main/java/com/qualimente/reliability/Example.java
@@ -1,11 +1,16 @@
package com.qualimente.reliability;
+import com.qualimente.reliability.web.ExampleController;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
+import org.springframework.roo.addon.json.RooJson;
import org.springframework.roo.addon.tostring.RooToString;
+import org.springframework.roo.addon.web.mvc.controller.json.RooWebJson;
@RooJavaBean
@RooToString
@RooJpaActiveRecord
+@RooJson
+@RooWebJson(jsonObject = ExampleController.class)
public class Example {
}
View
31 sample-webapp/src/main/java/com/qualimente/reliability/Example_Roo_Json.aj
@@ -0,0 +1,31 @@
+// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
+// You may push code into the target .java compilation unit if you wish to edit any member(s).
+
+package com.qualimente.reliability;
+
+import com.qualimente.reliability.Example;
+import flexjson.JSONDeserializer;
+import flexjson.JSONSerializer;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+privileged aspect Example_Roo_Json {
+
+ public String Example.toJson() {
+ return new JSONSerializer().exclude("*.class").serialize(this);
+ }
+
+ public static Example Example.fromJsonToExample(String json) {
+ return new JSONDeserializer<Example>().use(null, Example.class).deserialize(json);
+ }
+
+ public static String Example.toJsonArray(Collection<Example> collection) {
+ return new JSONSerializer().exclude("*.class").serialize(collection);
+ }
+
+ public static Collection<Example> Example.fromJsonArrayToExamples(String json) {
+ return new JSONDeserializer<List<Example>>().use(null, ArrayList.class).use("values", Example.class).deserialize(json);
+ }
+
+}
View
51 ...alimente/reliability/web/ApplicationConversionServiceFactoryBean_Roo_ConversionService.aj
@@ -1,51 +0,0 @@
-// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
-// You may push code into the target .java compilation unit if you wish to edit any member(s).
-
-package com.qualimente.reliability.web;
-
-import com.qualimente.reliability.Example;
-import com.qualimente.reliability.web.ApplicationConversionServiceFactoryBean;
-import org.springframework.beans.factory.annotation.Configurable;
-import org.springframework.core.convert.converter.Converter;
-import org.springframework.format.FormatterRegistry;
-
-privileged aspect ApplicationConversionServiceFactoryBean_Roo_ConversionService {
-
- declare @type: ApplicationConversionServiceFactoryBean: @Configurable;
-
- public Converter<Example, String> ApplicationConversionServiceFactoryBean.getExampleToStringConverter() {
- return new org.springframework.core.convert.converter.Converter<com.qualimente.reliability.Example, java.lang.String>() {
- public String convert(Example example) {
- return new StringBuilder().toString();
- }
- };
- }
-
- public Converter<Long, Example> ApplicationConversionServiceFactoryBean.getIdToExampleConverter() {
- return new org.springframework.core.convert.converter.Converter<java.lang.Long, com.qualimente.reliability.Example>() {
- public com.qualimente.reliability.Example convert(java.lang.Long id) {
- return Example.findExample(id);
- }
- };
- }
-
- public Converter<String, Example> ApplicationConversionServiceFactoryBean.getStringToExampleConverter() {
- return new org.springframework.core.convert.converter.Converter<java.lang.String, com.qualimente.reliability.Example>() {
- public com.qualimente.reliability.Example convert(String id) {
- return getObject().convert(getObject().convert(id, Long.class), Example.class);
- }
- };
- }
-
- public void ApplicationConversionServiceFactoryBean.installLabelConverters(FormatterRegistry registry) {
- registry.addConverter(getExampleToStringConverter());
- registry.addConverter(getIdToExampleConverter());
- registry.addConverter(getStringToExampleConverter());
- }
-
- public void ApplicationConversionServiceFactoryBean.afterPropertiesSet() {
- super.afterPropertiesSet();
- installLabelConverters(getObject());
- }
-
-}
View
18 sample-webapp/src/main/java/com/qualimente/reliability/web/ExampleController.java
@@ -1,12 +1,30 @@
package com.qualimente.reliability.web;
import com.qualimente.reliability.Example;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.roo.addon.web.mvc.controller.json.RooWebJson;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
@RequestMapping("/examples")
@Controller
@RooWebScaffold(path = "examples", formBackingObject = Example.class)
+@RooWebJson(jsonObject = Example.class)
public class ExampleController {
+
+ @RequestMapping(method = RequestMethod.POST, headers = "Accept=application/json")
+ public ResponseEntity<String> createFromJson(@RequestBody String json) {
+
+ Example example = Example.fromJsonToExample(json);
+ example.persist();
+ HttpHeaders headers = new HttpHeaders();
+ headers.add("Content-Type", "application/json");
+ return new ResponseEntity<String>(headers, HttpStatus.CREATED);
+ }
+
}
View
23 sample-webapp/src/test/java/com/qualimente/reliability/ExampleTest.java
@@ -0,0 +1,23 @@
+package com.qualimente.reliability;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+
+public class ExampleTest {
+
+ @Test
+ public void should_roundtrip_basic_json(){
+ String basicExampleJson = "{\"id\":1,\"version\":2}";
+ Example example = Example.fromJsonToExample(basicExampleJson);
+
+ assertNotNull(example);
+ assertEquals(Long.valueOf(1), example.getId());
+ assertEquals(Integer.valueOf(2), example.getVersion());
+
+ assertEquals(basicExampleJson, example.toJson());
+ }
+
+}

0 comments on commit 1c6ca14

Please sign in to comment.
Something went wrong with that request. Please try again.