I have received json data from android application in my Spring MVC controller using following code .
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestHeader;
@RequestMapping(method = RequestMethod.POST, produces = "application/json")
public @ResponseBody String getMethod(@RequestHeader(value="json") String
headerStr) {
System.out.println("POST");
System.out.println(headerStr);
return "hello";
}
The output of System.out.println(headerStr) is as
{"action":"check_login","password":"test","username":"test"}
But i want to bind this json data to following class.
public class LoginRequest {
private String action;
private String username;
private String password;
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
in my POM.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.1</version>
</dependency>
Following is my android code
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);
How to solve this problem?
Data binding is useful for allowing user input to be dynamically bound to the domain model of an application (or whatever objects you use to process user input). Spring provides the so-called DataBinder to do exactly that.
Data Binding API is used to convert JSON to and from POJO (Plain Old Java Object) using property accessor or using annotations. It is of two type. Simple Data Binding - Converts JSON to and from Java Maps, Lists, Strings, Numbers, Booleans and null objects. Full Data Binding - Converts JSON to and from any JAVA type.
Spring MVC provides one more annotation, @ModelAttributes , for binding data to the Command object. It is another way to bind the data and to customize the data binding. This annotation allows you to control the creation of the Command object.
Simply put, the @RequestBody annotation maps the HttpRequest body to a transfer or domain object, enabling automatic deserialization of the inbound HttpRequest body onto a Java object. Spring automatically deserializes the JSON into a Java type, assuming an appropriate one is specified.
You should change the arguments in your method to
public @ResponseBody String getMethod(@RequestBody LoginRequest loginRequest)
this way the LoginRequest
class will be instantiated and bound to json value where keys match the property
Also, make sure that you send the json as part of the body, add the following
StringEntity se = new StringEntity(json.toString(), "UTF8");
se.setHeader("Content-type", "application/json");
post.setEntity(se);
before the HttpResponse response = httpclient.execute(httppost);
line
Use the Jackson ObjectMapper:
ObjectMapper mapper = new ObjectMapper();
LoginRequest login = mapper.readValue(headerStr, LoginRequest.class);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With