Android Volley 请求 - 将对象发送到 Spring boot java web 服务

标签 android spring spring-boot android-volley

我有一些自定义的 POJO:

class CustomClass {
    int x;
    String str;
    SecondCustomClass obj;  //indicate it's not class of simple types  
    //etc...
}

我想将他的实例从 Android(Volley 库)客户端发送到在 Spring-boot java 应用程序上运行的 Web 服务。目前我知道如何使用 URL 参数发送数据并返回到客户端自定义对象。但我还想发送自定义对象。

Android 中的代码(我知道我需要使用现在为 null 的第 3 个参数,但我正在努力让它工作):

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
    "BASE_URL?param1=param", 
    null,
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            CustomClass result = new Gson().fromJson(response.toString(), CustomClass.class);
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }
);
volleyQueue.add(request);

Code in server:

编辑:接收 pojo 的解决方案是使用 @RequestBody

@RequestMapping("/webmethod")
public CustomClass webmethod(@RequestParam(value="obj_param") @RequestBody CustomClass obj) {
    //work with obj
}

What do I need to put in Android side to get it work?

最佳答案

您必须在 JSON 对象中使用 JSON 对象。下面是示例。当您仅通过一个请求请求参数时。

这是请求 JSON

  {
    "x":10,
     "str":"MyName",
     "SecondCustomClass":{
          "id":10,
           "title":"make it eassy"
        }  
   }

这是来自Android的post参数请求。试试这个方法。 更多详情 please use this link

关于Android Volley 请求 - 将对象发送到 Spring boot java web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34251321/

相关文章:

android - 在服务中调用 PhoneStateListener 时出现 NullPointerExecption

android - 使用接近传感器锁定和解锁主屏幕

java - 如何使用扩展类中的字段创建 HQL 查询

java - 泽西警告 - 无法解析为具体类型

java - 将 Spring Boot 1.3.2 升级到 1.4.1 后,Hibernate hbm2ddl (ddl-auto) 失败

java - spring-boot-starter-mail 上的 Spring Boot 读取和新邮件监听器

java - 如何修复Android Studio中的 ‘null json data ’错误

安卓图标没有出现

spring - 如何在 Spring Security 中允许单个 URL

java - 如何使用 Spring Boot 配置属性对属性进行分组