Android 向 spring 引导发送放置请求时出现改造错误?

Android retrofit error in sending put request to spring boot?

所以我正在做我的项目。我使用 spring boot 作为后端,android java 作为前端。基本上,我已经通过邮递员测试了我的端点并且它有效。但是当我尝试从我的 android 应用程序发送 json 数据时,它向我显示了 "Error Code 500: The given ID must not be null" 之类的错误。我的意思是,我已经通过 Log.d 检查了我的呼叫代码。这是我的放置请求方法:

 public void updateAddress(AddresslistResponse addresslistResponse){
        SharedPreferences preferences = getSharedPreferences("MyPref",0);
        String tokens = preferences.getString("userToken",null);
        Call<AddresslistResponse> call = mainInterface.editAddress("Bearer " + tokens, addresslistResponse);
        call.enqueue(new Callback<AddresslistResponse>() {
            @Override
            public void onResponse(Call<AddresslistResponse> call, Response<AddresslistResponse> response) {
                Log.d("Call request", call.request().toString());
                Log.d("Call request header", call.request().headers().toString());
                Log.d("Response raw header", response.headers().toString());
                Log.d("Response raw", String.valueOf(response.raw().body()));
                Log.d("Response code", String.valueOf(response.code()));

                try {
                    if(response.body()!=null)
                        Toast.makeText(EditDataAlamat.this," response message success "+response.body(),Toast.LENGTH_LONG).show();
                    if(response.errorBody()!=null)
                        Toast.makeText(EditDataAlamat.this," response message error "+response.errorBody().string(),Toast.LENGTH_LONG).show();

                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<AddresslistResponse> call, Throwable t) {
                Log.e("ERROR: ", t.getMessage());
            }
        });
    }

这是我的日志输出:

2020-06-16 11:25:06.278 27478-27478/com.example.my_app D/Call request: Request{method=PUT, url=http://localhost:1111/address/update/, tags={class retrofit2.Invocation=com.example.my_app .Interface.MainInterface.editAddress() [Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbldTSFJJUyIsInNjb3BlcyI6IlJPTEVfQURNSU4iLCJpYXQiOjE1OTIyNzkwODksImV4cCI6MTU5Mj, AddresslistResponse{alamat2 = 'Jl. mangga',telp2 = 'SOLO',telp1 = 'BANDUNG',kota2 = '16512',kota1 = '4568156',alamat1 = 'JL ramai',noPegawai = '14141414'}]}}
2020-06-16 11:25:06.279 27478-27478/com.example.my_app D/Call request header: Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbldTSFJJUyIsInNjb3BlcyI6IlJPTEVfQURNSU4iLCJpYXQiOjE1OTIyNzkwODksImV4cCI6MTU5MjM2NTQ4OX0.EdOBfplxy-RKfPcHA2vfk9PPvPv3UxoQua9ovKbgT7U
2020-06-16 11:25:06.279 27478-27478/com.example.my_app D/Response raw header: Content-Type: application/json;charset=UTF-8
    Transfer-Encoding: chunked
    Date: Tue, 16 Jun 2020 04:25:06 GMT
    Connection: close
2020-06-16 11:25:06.279 27478-27478/com.example.my_app D/Response raw: retrofit2.OkHttpCall$NoContentResponseBody@e938081
2020-06-16 11:25:06.280 27478-27478/com.example.my_app D/Response code: 500

根据上面显示的日志,我已经将值作为 JSON 对象传递。即使在邮递员中,我也成功地发送了这种 json 格式的放置请求:

{
    "noPegawai": "141414",
    "kota2": "SOLO",
    "telp2": "085264524",
    "alamat2": "Jl. mangga",
    "alamat1": "JL ramai",
    "kota1": "BANDUNG",
    "telp1": "0812856477"
}

我已经尝试像这样更改 spring 启动时的调用方法,但仍然显示相同的错误:

@Transactional
    @Override
    public Alamat updateAlamat(Alamat address) {
        Optional<Alamat> newAddress = alamatRepository.findById(address.getNik());
        newAddress.get().setNoPegawai(address.getNik());
        newAddress.get().setAlamat1(address.getAlamat1());
        newAddress.get().setAlamat2(address.getAlamat2());
        newAddress.get().setKota1(address.getKota1());
        newAddress.get().setKota2(address.getKota2());
        newAddress.get().setTelp1(address.getTelp1());
        newAddress.get().setTelp2(address.getTelp2());
        return alamatRepository.save(newAddress.get());
    }

我错过了什么吗?我已经尝试解决了 2 天,但仍然没有任何进展。请帮助我 :D 谢谢。

我找到了解决方法。因此,在我的模型 class 中,我根据 GET 请求的 JSON 格式初始化了字段名称,但它应该遵循 Put 请求的格式。 示例:

//field name for PUT method
@Serialized ("noPegawai")
private String id;

//field name for GET method
@Serialized ("NoPegawai")
private String id

我不知道为什么会出现这个问题,就在我的table字段上写的和"NoPegawai"一模一样,但是JAVA这里的东西只能读(使用 GET 方法)但无法将其发送到 PUT 方法并将其作为空值返回。所以这个解决方案是我创建了另一个模型 class 具有相同的初始化但具有不同的字段名称案例。结果,我有 2 个模型 classes,它们具有相同的字段,但只是大小写不同。一种用于 PUT 方法,另一种用于 GET 方法。

我不知道这是否适用于其他情况,但就我而言,它有效。如果有人能告诉我最好的解决方案,我将不胜感激:D