@ResponseBody
类型:方法注解 类注解
位置:Controller方法上或类上
作用:将方法返回值直接响应,如果返回值类型是实体对象/集合,将会转换为JSON格式响应
说明:@RestContorller=@Contorller+@ResponseBody
package com.ming.pojo; public class Address { private String province; private String city; public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } @Override public String toString() { return "Address{" + "province='" + province + '\'' + ", city='" + city + '\'' + '}'; } }
package com.ming.pojo; public class User { private String name; private int age; private Address address; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", address=" + address + '}'; } }
package com.ming.controller; import com.ming.pojo.Address; import com.ming.pojo.User; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class Requsetcontroller { @RequestMapping("ResponseBodyParam") public User ResponseBodyParam(User user){ Address address = new Address(); address.setProvince("河南"); address.setCity("郑州"); user.setName("张三"); user.setAge(18); user.setAddress(address); return user; } }
我们发现返回的数据类型是json
统一响应结果
首先是注入依赖
<!--alibaba的fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.78</version> </dependency> <!--swagger--> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.6.2</version> </dependency>
然后是创建三个类
分别是Result类
package com.ming.pojo; import com.alibaba.fastjson.JSON; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; /** * 统一API响应结果封装 */ @ApiModel(value = "Result", description = "统一返回结果") public class Result<T> { @ApiModelProperty(value = "状态码(200:成功,400:失败,401:无权限,404:接口不存在,500:服务器内部错误)") private int code; @ApiModelProperty(value = "状态信息") private String msg; @ApiModelProperty(value = "返回数据,一般为json对象或json数组") private T data; public Result setCode(ResultCode resultCode) { this.code = resultCode.code(); return this; } public int getCode() { return code; } public String getMsg() { return msg; } public Result setMsg(String msg) { this.msg = msg; return this; } public T getData() { return data; } public Result setData(T data) { this.data = data; return this; } @Override public String toString() { return JSON.toJSONString(this); } }
然后是ResultCode类
package com.ming.pojo; /** * 响应码枚举,参考HTTP状态码的语义 */ public enum ResultCode { SUCCESS(200),//成功 FAIL(400),//失败 UNAUTHORIZED(401),//未认证(签名错误) NOT_FOUND(404),//接口不存在 INTERNAL_SERVER_ERROR(500);//服务器内部错误 private final int code; ResultCode(int code) { this.code = code; } public int code() { return code; } }
然后是ResultGenerator类
package com.ming.pojo; /** * 响应结果生成工具 */ public class ResultGenerator { private static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS"; public static Result genSuccessResult() { return new Result() .setCode(ResultCode.SUCCESS) .setMsg(DEFAULT_SUCCESS_MESSAGE); } public static <T> Result<T> genSuccessResult(T data) { return new Result() .setCode(ResultCode.SUCCESS) .setMsg(DEFAULT_SUCCESS_MESSAGE) .setData(data); } public static <T> Result<T> genSuccessResult(String message, T data) { return new Result() .setCode(ResultCode.SUCCESS) .setMsg(message) .setData(data); } public static Result genFailResult(String message) { return new Result() .setCode(ResultCode.FAIL) .setMsg(message); } public static <T> Result<T> genFailResult(String message, T data) { return new Result() .setCode(ResultCode.FAIL) .setMsg(message) .setData(data); } }
关于Result的使用
package com.ming.controller; import com.ming.pojo.Address; import com.ming.pojo.Result; import com.ming.pojo.ResultGenerator; import com.ming.pojo.User; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class Requsetcontroller { @RequestMapping("ResponseBodyParam") public Result ResponseBodyParam(User user){ Address address = new Address(); address.setProvince("河南"); address.setCity("郑州"); user.setName("张三"); user.setAge(18); user.setAddress(address); return ResultGenerator.genSuccessResult(user); } }
运行之后在postman里面查看可知导入成功