[英]Spring Rest return a JSON response with a certain http response code
I am very new to Spring. 我是Spring的新手。 I have a REST api written in Spring, but I don't know how to return a JSON response with a custom http response code.
我有一个用Spring编写的REST api,但是我不知道如何返回带有自定义http响应代码的JSON响应。
I return a JSON response as follows: 我返回一个JSON响应,如下所示:
public String getUser(String id){
...
return jsonObj;
}
But it always displays 200 http ok status code. 但它始终显示200 http ok状态代码。
Here are my questions: 这是我的问题:
How can I synchronize the response JSON and HTTP code? 如何同步响应JSON和HTTP代码?
How is it possible to return JSON response and custom HTTP code in void function? 在void函数中如何返回JSON响应和自定义HTTP代码?
Use @ResponseStatus
annotation: 使用
@ResponseStatus
批注:
@GetMapping
@ResponseStatus(HttpStatus.ACCEPTED)
public String getUser(String id) {...}
Alternative way: If you want to decide programmatically what status to return you can use ResponseEntity
. 另一种方法:如果要以编程方式决定要返回什么状态,可以使用
ResponseEntity
。 Change return type of a method to ResponseEntity<String>
and you'll be offered with a DSL like this: 将方法的返回类型更改为
ResponseEntity<String>
,将为您提供像这样的DSL:
ResponseEntity
.status(NOT_FOUND)
.contentType(TEXT_PLAIN)
.body("some body");
Here is how I do JSON returns from a Spring Handler method. 这是我的工作方式,即从Spring Handler方法返回JSON。 My techniques are somewhat out-of-date, but are still reasonable.
我的技术有些过时,但仍然合理。
Configure Jackson Add the following to the spring configuration xml file: 配置Jackson将以下内容添加到spring配置xml文件中:
<bean name="jsonView"
class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
</bean>
With that, Spring will convert return values to JSON and place them in the body of the response. 这样,Spring会将返回值转换为JSON并将其放置在响应的正文中。
Create a utility method to build the ResponseEntity Odds are good that you will have multiple handler methods. 创建一个实用程序方法来构建ResponseEntity的好处是,您将拥有多个处理程序方法。 Instead of boilerplate code, create a method to do the standard work.
代替样板代码,创建一种方法来执行标准工作。
ResponseEntity
is a Spring class. ResponseEntity
是一个Spring类。
protected ResponseEntity<ResponseJson> buildResponse(
final ResponseJson jsonResponseBody,
final HttpStatus httpStatus)
{
final ResponseEntity<ResponseJson> returnValue;
if ((jsonResponseBody != null) &&
(httpStatus != null))
{
returnValue = new ResponseEntity<>(
jsonResponseBody,
httpStatus);
}
return returnValue;
}
Annotate the handler method 注释处理程序方法
@RequestMapping(value = "/webServiceUri", method = RequestMethod.POST)
you can also use the @PostMethod
annotation 您还可以使用
@PostMethod
批注
@PostMethod("/webServiceUri")
Return ResponseEntity
from the handler method Call the utility method to build the ResponseEntity
从处理程序方法返回
ResponseEntity
调用实用程序方法以构建ResponseEntity
public ResponseEntity<ResponseJson> handlerMethod(
... params)
{
... stuff
return buildResponse(json, httpStatus);
}
Annotate the handler parameters Jackson will convert from json to the parameter type when you use the @RequestBody
annotation. 注释处理程序参数当您使用
@RequestBody
注释时,Jackson将从json转换为参数类型。
public ResponseEntity<ResponseJson> handlerMethod(
final WebRequest webRequest,
@RequestBody final InputJson inputJson)
{
... stuff
}
You can use the @JsonView
annotation. 您可以使用
@JsonView
批注。 Check out the Spring Reference for details about this. 查看Spring Reference以获得有关此内容的详细信息。 Browse to the ref page and search for @JsonView.
浏览到参考页面并搜索@JsonView。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.