Spring mvc中的JSON数据绑定

Sur*_*h A 9 java data-binding json spring-mvc

我使用以下代码从我的Spring MVC控制器中的android应用程序接收了json数据.

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";
}
Run Code Online (Sandbox Code Playgroud)

System.out.println(headerStr)的输出为

{"action":"check_login","password":"test","username":"test"}
Run Code Online (Sandbox Code Playgroud)

但我想将这个json数据绑定到下面的类.

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;
}
Run Code Online (Sandbox Code Playgroud)

在我的POM.xml中

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

以下是我的Android代码

    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);
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题呢?

Mas*_*ave 9

您应该将方法中的参数更改为

public @ResponseBody String getMethod(@RequestBody LoginRequest loginRequest) 
Run Code Online (Sandbox Code Playgroud)

这样,LoginRequest类将被实例化并绑定到json值,其中键与属性匹配

此外,请确保将json作为正文的一部分发送,添加以下内容

StringEntity se = new StringEntity(json.toString(), "UTF8");  
se.setHeader("Content-type", "application/json");
post.setEntity(se);
Run Code Online (Sandbox Code Playgroud)

该前HttpResponse response = httpclient.execute(httppost);

  • 标题用于元数据,传达有关客户端浏览器,请求的页面,服务器这类东西的信息.您目前的情况是一个很好的例子,如果您要传递正确的标头元数据,例如"Content-type","application/json",您可以向服务器提供正文消息内容类型的信息,启用它应用自动转换,由@@ RequestBody`处理的东西.如果您通过标题传递数据,就像通过窗口进入建筑物一样,您可以这样做,但它并不意味着这一点,因此您必须依靠手动执行所有处理. (3认同)

Ste*_*fan 2

使用杰克逊对象映射器

ObjectMapper mapper = new ObjectMapper();
LoginRequest login = mapper.readValue(headerStr, LoginRequest.class);
Run Code Online (Sandbox Code Playgroud)