I am new to using Spring mvc4 annotation . In all i want to do is using spring mvc as a web service . so i would be thankful if anyone could provide me a solution for it.
My android code is as:
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, 5000);
HttpClient httpclient = new DefaultHttpClient(httpParams);
HttpPost httppost = new HttpPost( "http://localhost:8080/datarequest");
JSONObject json = new JSONObject();
json.put("action", "check_login");
json.put("username","name");
json.put("password", "password");
JSONArray postjson = new JSONArray();
postjson.put(json);
httppost.setHeader("json", json.toString());
httppost.getParams().setParameter("jsonpost", postjson);
System.out.println(postjson);
HttpResponse response = httpclient.execute(httppost);
so far i wrote the web service as:
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/datarequest")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String getMethod(ModelMap model) {
System.out.println("GET");
return "hello";
}
@RequestMapping(method = RequestMethod.POST, produces = "application/json")
public String getMethod(ModelMap model) {
System.out.println("POST");
System.out.println("here i want to print json data send from the android ");
return "hello";
}
}
You can use
@RequestBody
and map it to required class structure. You can refer this SO question @RequestBody and @ReponseBody spring.To test, you can map it to String. See below example.
You can add below maven entry in your pom.