displayname displayname - 1 year ago 80
reST (reStructuredText) Question

Simple REST API calls using Spring?

Okay I'm having a simple REST API that is now running. I also got a mobile app (Android) that would be ready for some network communication. But here is the thing:

I now got a fancy API created with Spring that I can use with Http-Requests:

package com.helloworld

public class HelloWorldController {

@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(ModelMap model) {
model.addAttribute("msg", "JCG Hello World!");
return "helloWorld";

This is all fine but how would I use this API in my Android mobile App? What I mean is that I don't know how to make those calls more elegantly and simple! I don't want to write for every request on my API something like this:

class HelloWorldTask extends AsyncTask<String, String, String> {

private String url = "" + msg;

protected String doInBackground(String... uri) {

String msg = "";
for(String m : uri) { msg += m; }

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
return restTemplate.getForObject(url, String.class, "Android");

Is there any way to avoid this? To be honest I believed that I would be able to just import something like a
after building my REST API project and then do something like this:

import com.helloworld

public void getServerHello() {

HelloWorldController api;

HelloWorld helloWorld = api.getHelloWorldMapping();
helloWorld.displayMessage("How are you?");

And that this will be it. So is there a way to do something like this when writing a Spring REST API or will I really have to use something like a URI-builder and create for all kind of requests an additional
class <whatever>Task extends AsyncTask<?, ?, ?>
by myself?

Since the API is clearly defined it should be possible to do something like this, but is it?

I hope my question is clear - please let me know if not.

Answer Source

In my current project we have similar setting with a Java client and a RESTful backend based on Springs REST API. We use apache-cxf-rs-client-api to call our REST services from the clients.

  1. Create an Interface for your Methods in your HelloWorldController.

  2. Add apache-cxf-rs-client-api to your project (take a look for more information).

  3. Now you can call the interface on the client and Apache CXF will do the rest for you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download