Announcement Announcement Module
Collapse
No announcement yet.
Json all and Spring android Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Json all and Spring android

    Hi all .
    I used json all to generate Restful services and Use Spring android to access the services.
    The serverside code
    ToppingController_Roo_Controller_Json.aj
    Code:
      @RequestMapping(headers = "Accept=application/json")
        @ResponseBody
        public ResponseEntity<String> ToppingController.listJson() {
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "application/text; charset=utf-8");
            return new ResponseEntity<String>(Topping.toJsonArray(Topping.findAllTimers()), headers, HttpStatus.OK);
        }
    And the android side.
    Code:
    HttpHeaders requestHeaders = new HttpHeaders();
    List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
    	acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
    //	acceptableMediaTypes.add(new MediaType("application","text",Charset.forName("UTF-8")));
    				
    	requestHeaders.setAccept(acceptableMediaTypes);
    	// Populate the headers in an HttpEntity object to use for the
    	// request
    	HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
    				
    	// Create a new RestTemplate instance
    	RestTemplate restTemplate = new RestTemplate();
          // Perform the HTTP GET request
    	ResponseEntity<Topping[]> responseEntity = restTemplate
    		.exchange(url, HttpMethod.GET, requestEntity,Topping[].class);
    	// convert the array to a list and return it
    	return Arrays.asList(responseEntity.getBody());
    As you see the serverside add some code as
    Code:
     HttpHeaders headers = new HttpHeaders();
           headers.add("Content-Type", "application/text; charset=utf-8");
    So the android client can not parse the json and the error is
    06-01 12:36:10.081: ERROR/HttpGetJsonToppingsActivity(667): org.springframework.web.client.RestClientException : Could not extract response: no suitable HttpMessageConverter found for response type [[Lorg.springframework.android.showcase.rest.Topping ;] and content type [application/text;charset=utf-8]
    06-01 12:36:10.081: ERROR/HttpGetJsonToppingsActivity(667): at org.springframework.web.client.HttpMessageConverte rExtractor.extractData(HttpMessageConverterExtract or.java:77)
    06-01 12:36:10.081: ERROR/HttpGetJsonToppingsActivity(667): at org.springframework.web.client.RestTemplate$Respon seEntityResponseExtractor.extractData(RestTemplate .java:632)
    06-01 12:36:10.081: ERROR/HttpGetJsonToppingsActivity(667): at org.springframework.web.client.RestTemplate$Respon seEntityResponseExtractor.extractData(RestTemplate .java:618)
    06-01 12:36:10.081: ERROR/HttpGetJsonToppingsActivity(667): at org.springframework.web.client.RestTemplate.doExec ute(RestTemplate.java:459)
    06-01 12:36:10.081: ERROR/HttpGetJsonToppingsActivity(667): at org.springframework.web.client.RestTemplate.execut e(RestTemplate.java:414)
    06-01 12:36:10.081: ERROR/HttpGetJsonToppingsActivity(667): at org.springframework.web.client.RestTemplate.exchan ge(RestTemplate.java:390)
    06-01 12:36:10.081: ERROR/HttpGetJsonToppingsActivity(667): at org.springframework.android.showcase.rest.HttpGetJ sonToppingsActivity$DownloadStatesTask.doInBackgro und(HttpGetJsonToppingsActivity.java:107)
    06-01 12:36:10.081: ERROR/HttpGetJsonToppingsActivity(667): at org.springframework.android.showcase.rest.HttpGetJ sonToppingsActivity$DownloadStatesTask.doInBackgro und(HttpGetJsonToppingsActivity.java:76)
    06-01 12:36:10.081: ERROR/HttpGetJsonToppingsActivity(667): at android.os.AsyncTask$2.call(AsyncTask.java:185)
    06-01 12:36:10.081: ERROR/HttpGetJsonToppingsActivity(667): at java.util.concurrent.FutureTask$Sync.innerRun(Futu reTask.java:306)
    06-01 12:36:10.081: ERROR/HttpGetJsonToppingsActivity(667): at java.util.concurrent.FutureTask.run(FutureTask.jav a:138)
    06-01 12:36:10.081: ERROR/HttpGetJsonToppingsActivity(667): at java.util.concurrent.ThreadPoolExecutor.runWorker( ThreadPoolExecutor.java:1088)
    06-01 12:36:10.081: ERROR/HttpGetJsonToppingsActivity(667): at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:581)
    06-01 12:36:10.081: ERROR/HttpGetJsonToppingsActivity(667): at java.lang.Thread.run(Thread.java:1019)

    After I deleted the
    Code:
     HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "application/text; charset=utf-8");
    at server side, everything goes well.

    But Roo Reference book doesn't recommend modify the *.aj file..
    Any other suggestions to solve this issue.

    How do you make Spring Roo and Spring Android work in pair??

  • #2
    Anything you need to modify in the .aj file can be modified, but you should put the modified method (cut and paste from the .aj file) into the class. Once the method exists in the class, Roo will delete (or no longer create) the method in the .aj file allowing you to persist your changes to the Roo generated code.

    I just did this for some unit tests which were failing due to the @Email annotation (the Roo code was creating an entity with an email address which did not pass @Email validation).

    Comment


    • #3
      There are quite a few tricky subtelties involved when using RestTemplate and the autogenerated MVC controllers together. A lot of the content negotiation going on is being done automatically based on common assumptions, but you have to dig down in the code to find them. Different parameters on either send, (String vs Object vs Pojo) cause different encodings to be set, and its all very useful when a browser is talking to your Controller, and can be a little more frustrating when trying to use a RestTemplate to do the same thing.

      My suggestion would be to add the data pojos to a common jar that both ends use, and send and receive those with the JSON being done automatically by the Spring Jackson integration.

      eg.
      Code:
      @ResponseBody ResponseEntity<SomePojo> myMethod()

      Comment

      Working...
      X