2015-06-16 24 views
9

Tôi đã nhận dữ liệu json từ ứng dụng android trong bộ điều khiển MVC Spring của tôi bằng cách sử dụng mã sau.Ràng buộc dữ liệu JSON vào mùa xuân 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"; 
} 

Kết quả của System.out.println (headerStr) là như

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

Nhưng tôi muốn để ràng buộc dữ liệu json này để sau lớp.

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

trong pom.xml của tôi

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

Tiếp theo là mã android tôi

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); 

Làm thế nào để giải quyết vấn đề này?

Trả lời

1

Sử dụng Jackson ObjectMapper:

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

Nhờ nó hoạt động @Stefan Lindenberg –

9

Bạn nên thay đổi các đối số trong phương pháp của bạn để

public @ResponseBody String getMethod(@RequestBody LoginRequest loginRequest) 

cách này lớp LoginRequest sẽ được khởi tạo và gắn kết với giá trị json nơi các khóa khớp với tài sản

Ngoài ra, hãy đảm bảo rằng bạn gửi json như một phần của phần thân, thêm

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

trước dòng HttpResponse response = httpclient.execute(httppost);

+1

cũng cần phải chắc chắn rằng Jackson dependeny đang có http://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind /2.5.4 –

+0

thêm mã máy khách, phần mà bạn gửi yêu cầu –

+0

tôi đã cập nhật mã của mình ở trên nhưng vẫn không nhận được đầu ra ??? @Master Slave –

Các vấn đề liên quan