在 Retrofit 的 POST 请求中发送空正文

fwi*_*ind 5 java request jackson retrofit

我的 api{ }在发出帖子请求时需要一个空的 json 主体 ( )。我如何在 Retrofit 和 Jackson 中进行设置?

我尝试传递null, 和空字符串,"{}"但无法使其正常工作。

@POST(my/url)
Call<MyResponse> createPostRequest(@Body Object empty);
Run Code Online (Sandbox Code Playgroud)

如何设置空的 JSON 正文?

小智 11

一个空对象可以为Kotlin做到这一点:

interface ApiService {
    @POST("your.url")
    fun createPostRequest(@Body body: Any = Object()): Call<YourResponseType>
}
Run Code Online (Sandbox Code Playgroud)


Gau*_*mar 5

尝试这个 。它现在对我有用。

@POST(my/url)
Call<MyResponse> createPostRequest(@Body Hashmap );
Run Code Online (Sandbox Code Playgroud)

使用此方法new HasMap作为参数传递时

apiservice.createPostRequest(new HashMap())
Run Code Online (Sandbox Code Playgroud)


led*_*iov 4

空类就可以解决问题:

class EmptyRequest {
    public static final EmptyRequest INSTANCE = new EmptyRequest();
}

interface My Service {

    @POST("my/url")
    Call<MyResponse> createPostRequest(@Body EmptyRequest request);

}

myService.createPostRequest(EmptyRequest.INSTANCE);
Run Code Online (Sandbox Code Playgroud)