Java restapi接受Blob输入
我的产品中有一个搜索功能,用户可以键入任何他想要的内容。 这个字符串可以是多行的 输入多行数据(字符串)后,用户点击“搜索”按钮。 这将把“blob”发送到我的Api 现在我相信这将是一个POST请求。(如果我错了,请纠正我) 但我不确定接收输入的最佳数据结构是什么 (我正在使用dropwizard) 我应该如何将输入发送到我的api?因此,我的意见应该是:Java restapi接受Blob输入,java,api,rest,blob,Java,Api,Rest,Blob,我的产品中有一个搜索功能,用户可以键入任何他想要的内容。 这个字符串可以是多行的 输入多行数据(字符串)后,用户点击“搜索”按钮。 这将把“blob”发送到我的Api 现在我相信这将是一个POST请求。(如果我错了,请纠正我) 但我不确定接收输入的最佳数据结构是什么 (我正在使用dropwizard) 我应该如何将输入发送到我的api?因此,我的意见应该是: 串 字符串数组(其中每行是数组的一个元素) ? 我当前正在将Blob数据作为POST请求有效负载的一部分发送。您没有“Blob”。blob
我当前正在将Blob数据作为POST请求有效负载的一部分发送。您没有“Blob”。blob是一个文本,而
textarea
的内容肯定不是二进制的,而是简单的文本
Java JAX-RS REST API可以接收HTTP POST的主体:
@POST
public Respone postSearch(@RequestBody String searchFieldContent) {
// do search
// return Response
}
我建议不要使用POST进行搜索(这将是RPC),而是使用GET。构建一个查询参数,如
GET /api/things?search=what the user entered into the textarea
可以映射到
@GET
public Response searchThings(@RequestParam("search") String searchParam) {
// do search
// return Response
}
编辑
使用POST进行搜索不是RESTful的。如果不能使用GET,更好的方法是将搜索建模为具有状态的独立资源
客户:
POST /api/searches
the long text the user entered in the textarea
服务器:
201 Created
Location: /api/searches/id-of-search-created-by-server
200 OK
Content-Type: application/json
{
"query": "the long text the user entered in the textarea",
"state", "finished",
"result": {
"length": 23,
"documents: [
{
"id": "id of first search hit",
"title": "some title"
},
...
]
}
}
客户
GET /api/searches/id-of-search-created-by-server
服务器:
201 Created
Location: /api/searches/id-of-search-created-by-server
200 OK
Content-Type: application/json
{
"query": "the long text the user entered in the textarea",
"state", "finished",
"result": {
"length": 23,
"documents: [
{
"id": "id of first search hit",
"title": "some title"
},
...
]
}
}
你为什么要改变目前的做法?将搜索条件作为字符串发送非常有意义。通信协议应该正确处理需要转义的内容。我使用post的唯一原因是用户可能输入长字符串。我不想指定限制。所以你可以想象字符串也有10000个字符。我编辑了我的答案,向你展示了一种RESTful方法。