简体   繁体   English

必需的String参数不存在

[英]Required String parameter is not present

I want to send post request from my Android apps to Spring Boot. 我想从我的Android应用程序发送帖子请求到Spring Boot。 I use okhttp to send the HTTP post request in JSON. 我使用okhttp以JSON发送HTTP post请求。 The code is like this: 代码是这样的:

Every time I send post request using the Android request I got 400 bad request parameter 'name' is not present","path":"/newcustomer". But when I use postman it works. 每当我使用Android请求发送帖子请求时,我得到400个错误的请求参数“名称”不存在“,”路径“:”/ newcustomer“。但是当我使用邮递员时它可以工作。

    Java
    ----------------------------------------------------------------
    Log.d("okhttphandleruserreg", "called");
    MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    JSONObject jsonObject = new JSONObject();
    try{
        jsonObject.put("name", name);
        jsonObject.put("email", email);
        jsonObject.put("username", username);
        jsonObject.put("password", password);
        jsonObject.put("month", month);
        jsonObject.put("dayOfMonth", dayOfMonth);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    RequestBody body = RequestBody.create(JSON, jsonObject.toString());

    Request.Builder builder = new Request.Builder();
    builder.url(params[0]);
    builder.post(body);
    Request request = builder.build();

    Spring Boot
    -----------------------------------------------------------------
    @RequestMapping(value = "/newcustomer", method= RequestMethod.POST)
    public Customer newCust(@RequestParam(value="name") String name,
                        @RequestParam(value="email") String email,
                        @RequestParam(value="username") String username,
                        @RequestParam(value="password") String password,
                        @RequestParam(value="month") int month,
                        @RequestParam(value="dayOfMonth") int dayOfMonth
)

You are using Request Params in Spring Boot but whereas in Android code you sending that as Request Body. 您在Spring Boot中使用Request Params,但在Android代码中,您将其作为Request Body发送。

Please change any one of the above. 请更改上述任何一项。 Better if you use RequestBody in both places. 如果您在两个地方都使用RequestBody,那就更好了。

class Customer
{
String name:
String email:
String username;
String password;
int month;
int dayofthemonth;

//getter and setters
}

public Customer newCust(@RequestBody Customer newcustomer)
{
}

The way you have implemented your back-end /newcustomer API suggests you are expecting the request payload to be raw request params within the request form data. 您实现后端/新客户 API的方式表明您希望请求有效负载是请求表单数据中的原始请求参数。

Assuming the server side API is your contract, thus should remain as is, your client code should be updated as follows: 假设服务器端API是您的合同,因此应保持原样,您的客户端代码应更新如下:

Log.d("okhttphandleruserreg", "called");

// here you create your request body as as a Form one
RequestBody formBody = new FormBody.Builder()
  .add("name", "test")
  .add("email", "test@domain.com")
  .add("username", "test")
  .add("username", "test")
  .add("month", "january")
  .add("dayOfMonth", "1")
  .build();

Request request = new Request.Builder()
  .url(params[0])
  .post(formBody)
  .build();

// call your request

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM