为什么Resonse.ok().build()返回Response实体本身? [英] why Resonse.ok().build() return the Response entity itself?
问题描述
我有一个简单的REST API,由于该函数的主体是异步的,因此我只返回了Resonse.ok().build()
I have a simple REST API for which I am just returning Resonse.ok().build(), since the body of the function is asynchronous
我原本以为200状态代码的响应是空的,但是我却获得了对似乎是实体的Response调用的完整描述.
I was expecting an empty response with a 200 http status code, but instead I got a full description of what seems to be the Response calls as entity.
我做错了什么?
这是我从API调用中收到的json响应
Here is the json response that I received from my API call
{
"context": {
"headers": {},
"entity": null,
"entityType": null,
"entityAnnotations": [],
"entityStream": {
"committed": false,
"closed": false
},
"length": -1,
"language": null,
"location": null,
"committed": false,
"mediaType": null,
"allowedMethods": [],
"links": [],
"entityTag": null,
"stringHeaders": {},
"lastModified": null,
"date": null,
"acceptableMediaTypes": [
{
"type": "*",
"subtype": "*",
"parameters": {},
"quality": 1000,
"wildcardType": true,
"wildcardSubtype": true
}
],
"acceptableLanguages": [
"*"
],
"entityClass": null,
"responseCookies": {},
"requestCookies": {},
"lengthLong": -1
},
"status": 200,
"length": -1,
"language": null,
"location": null,
"metadata": {},
"cookies": {},
"mediaType": null,
"allowedMethods": [],
"links": [],
"statusInfo": "OK",
"entityTag": null,
"stringHeaders": {},
"entity": null,
"lastModified": null,
"date": null,
"headers": {}
}
REST API看起来像这样
the REST api looks like this
@RestController
@RequestMapping("/accountworkers")
@Api(value = "/updater")
public class AccountUpdater {
private static Logger LOGGER = LoggerFactory.getLogger(AccountUpdater.class);
@Autowired
private AccountUpdaterController auController;
@RequestMapping(value = "/updater", method = RequestMethod.GET)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response runUpdater() {
LOGGER.info("Running account updater");
auController.doAccountUpdateAsync();
return Response.ok().build();
}
}
推荐答案
您正在尝试混合使用JAX-RS和Spring MVC.这些不是同一件事,并且不兼容.通过返回Spring MVC无法识别的Response
,它像对待其他实体一样对它进行序列化.如果您使用的是Spring MVC,则希望使用 ResponseEntity
.
You are trying to mix JAX-RS and Spring MVC. These are not the same thing and are not compatible. By returning Response
, which Spring MVC doesn't recognize, it it serializing it like it would any other entity. If you are using Spring MVC, then you want to be using ResponseEntity
.
return ResponseEntity.ok().build()
如果您使用的是Spring MVC,则应删除Jersey/JAX-RS的所有依赖项,以免使您对可以使用和不能使用的内容感到困惑.
If you are using Spring MVC, you should remove any dependencies for Jersey/JAX-RS so you don't get confused as to what you can and cannot use.
此外,@Produces
也适用于JAX-RS.对于Spring MVC,您应该在
Aside, @Produces
is also for JAX-RS. For Spring MVC, you are supposed to add the produces inside the @RequestMapping
annotation.
@RequestMapping(value="/", produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
这篇关于为什么Resonse.ok().build()返回Response实体本身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!