9

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?

2 Answers 2

9

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

Sign up to request clarification or add additional context in comments.

7 Comments

Also need to make sure Jackson dependeny is there mvnrepository.com/artifact/com.fasterxml.jackson.core/…
add the client code, the part where you send a request
where to add those lines in my code? it would be great if you could provide the changes in server side rather in android side @Master Slave
The problem is that your data is packaged inside the header on the client side. If you don't want or can't change the client side, than you should follow Stefan's solution, so LoginRequest login = mapper.readValue(headerStr, LoginRequest.class);. But the data your posting should never be inside the header, that is why I'm suggesting both the change on the android and server side. The code should work out for you just fine, if not, edit the question with your current code, and I can help you debug
headers are meant for meta data, conveying information about the client browser, the requested page, the server this kind of stuff. Your current situation is a perfect example, if you were to pass proper header meta data e.g. "Content-type", "application/json" you can give an info to a server of the content-type of the body message, enabling it to apply an automatic conversion, the stuff handled by @RequestBody. If you pass the data via headers,its like entering the building through a window, you can do it, but its not meant for that, so you must count on doing all the processing manually.
|
2

Use the Jackson ObjectMapper:

ObjectMapper mapper = new ObjectMapper();
LoginRequest login = mapper.readValue(headerStr, LoginRequest.class);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.