响应数据
如何加载响应数据呢
其实在SpringBoot,已经有名为@RessponseBody的方法注解为我们提供的响应的方法,他的作用是将方法返回值直接响应,如果返回值类型为实体对象/集合,则会转换为JSON格式响应。
而@RestController已经在内部集成好了@RessponseBody。
我们可以看到@RestController的定义,他已经在内部同时集成了Controller和ResponseBody
@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Documented
@org.springframework.stereotype.Controller
@org.springframework.web.bind.annotation.ResponseBody
public @interface RestController {@org.springframework.core.annotation.AliasFor(annotation = org.springframework.stereotype.Controller.class)java.lang.String value() default "";
}
我们分别以三种形式建立响应,分别是hello()字符串、getAddr()对象(JSON)、getAddrList()集合数组(数组-JSON)。
package com.ztt.controller;import com.ztt.Pojo.Address;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
import java.util.List;@RestController
public class ResponseController {@RequestMapping("/hello") // 浏览器请求/hello就会自动调用下面的方法public String hello(){System.out.println("Hello World");return "Hello World";}@RequestMapping("/getAddr")public Address getAddr(){Address addr = new Address();addr.setCity("西安");addr.setProvince("陕西");return addr;}@RequestMapping("/getAddrList")public List<Address> listAddr(){List<Address> list = new ArrayList<>();Address addr = new Address();addr.setCity("西安");addr.setProvince("陕西");Address addr2 = new Address();addr.setCity("深圳");addr.setProvince("广东");list.add(addr);list.add(addr2);return list;}}
响应结果为:
统一的响应数据
刚刚上面我们已经实现了数据的响应,但是我们响应结果很复杂,包括了三种变量的响应,这其实不利于我们对接口的管理;一般工程级的项目需要提供统一的响应管理来对结构进行构造。
为此我们在com.ztt.Pojo包下,创建了Result类,其将为我们提供code、msg(返回信息)、data(响应体数据)
package com.ztt.Pojo;public class Result {private Integer code; // 1表示成功,0表示失败private String msg; //返回提示信息private Object data; // 返回具体到响应体的数据public Result(){}public Integer getCode() {return code;}public Result(Integer code,String msg,Object data){this.code = code;this.msg = msg;this.data = data;}public void setCode(Integer code) {this.code = code;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}// 快速创建的静态方法public static Result success(Object data){return new Result(1,"success",data);}public static Result success(){return new Result(1,"success",null);}public static Result error(String msg){return new Result(0,msg,null);}@Overridepublic String toString() {return "Result{" +"code=" + code +", msg='" + msg + '\'' +", data=" + data +'}';}
}
重写上面的响应
package com.ztt.controller;import com.ztt.Pojo.Address;
import com.ztt.Pojo.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
import java.util.List;@RestController
public class ResponseController {@RequestMapping("/hello") // 浏览器请求/hello就会自动调用下面的方法public Result hello(){System.out.println("Hello World");return new Result(1,"success","Hello World"); //传统的方式新建对象并返回}@RequestMapping("/getAddr")public Result getAddr(){Address addr = new Address();addr.setCity("西安");addr.setProvince("陕西");return Result.success(addr); // 用静态方式快速构建}@RequestMapping("/getAddrList")public Result listAddr(){List<Address> list = new ArrayList<>();Address addr = new Address();addr.setCity("西安");addr.setProvince("陕西");Address addr2 = new Address();addr.setCity("深圳");addr.setProvince("广东");list.add(addr);list.add(addr2);return Result.success(list);}}
响应结果