2015-06-16 17 views
9

Ich habe Json Daten von Android-Anwendung in meinem Spring MVC Controller mit folgendem Code erhalten.JSON Datenbindung im Frühjahr mvc

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"; 
} 

Die Ausgabe von System.out.println (headerStr) ist als

{"action":"check_login","password":"test","username":"test"} 

Aber ich möchte folgende Klasse dieses json Daten binden.

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 meinem pom.xml

<dependency> 
<groupId>com.fasterxml.jackson.core</groupId> 
<artifactId>jackson-databind</artifactId> 
<version>2.5.1</version> 
</dependency> 

Es folgt mein 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); 

Wie dieses Problem zu lösen?

Antwort

1

Verwenden Sie die Jackson ObjectMapper:

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

Dank es funktioniert @Stefan Linden –

9

Sie sollten die Argumente in Ihrer Methode zu

public @ResponseBody String getMethod(@RequestBody LoginRequest loginRequest) 

diese Art und Weise ändern, die LoginRequest Klasse wird auf json Wert instanziiert und gebunden werden, in dem Schlüssel die Eigenschaft übereinstimmen

Stellen Sie außerdem sicher, dass Sie den JSON als Teil des Gehäuses senden, fügen Sie die folgenden

hinzu
StringEntity se = new StringEntity(json.toString(), "UTF8"); 
se.setHeader("Content-type", "application/json"); 
post.setEntity(se); 

vor dem HttpResponse response = httpclient.execute(httppost); Linie

+1

außerdem sicher, dass Jackson dependeny machen müssen, ist es http://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind /2.5.4 –

+0

fügen Sie den Client-Code, den Teil, wo Sie eine Anfrage senden –

+0

Ich aktualisierte meinen Code oben, aber immer noch nicht Ausgabe ??? @Master Slave –