为什么 Resonse.ok().build() return Response 实体本身?
why Resonse.ok().build() return the Response entity itself?
我有一个简单的 REST API,我只是返回 Resonse.ok().build(),因为函数体是异步的
我原本期待一个带有 200 http 状态代码的空响应,但我却得到了似乎是作为实体的响应调用的完整描述。
我做错了什么?
这是我从 API 电话中收到的 json 回复
{
"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 看起来像这样
@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
.
return ResponseEntity.ok().build()
如果您正在使用 Spring MVC,您应该删除 Jersey/JAX-RS 的所有依赖项,这样您就不会对可以使用和不能使用的内容感到困惑。
此外,@Produces
也适用于 JAX-RS。对于 Spring MVC,您应该在 @RequestMapping
注释中添加产品。
@RequestMapping(value="/", produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
我有一个简单的 REST API,我只是返回 Resonse.ok().build(),因为函数体是异步的
我原本期待一个带有 200 http 状态代码的空响应,但我却得到了似乎是作为实体的响应调用的完整描述。
我做错了什么?
这是我从 API 电话中收到的 json 回复
{
"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 看起来像这样
@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
.
return ResponseEntity.ok().build()
如果您正在使用 Spring MVC,您应该删除 Jersey/JAX-RS 的所有依赖项,这样您就不会对可以使用和不能使用的内容感到困惑。
此外,@Produces
也适用于 JAX-RS。对于 Spring MVC,您应该在 @RequestMapping
注释中添加产品。
@RequestMapping(value="/", produces=MediaType.APPLICATION_JSON_UTF8_VALUE)