响应数据web

响应数据web_第1张图片

响应数据web_第2张图片

 get请求

package com.example.demo.controller.poio;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

//字符串格式返回
@RestController
public class ResponseBody {
    @RequestMapping("/hello")
    public String hello() {
        String st = "hello world ~";
        System.out.println(st);
        return st;
    }

    //json 格式返回
    @RequestMapping("/getaddr")
    public Address getaddr() {
        Address addr = new Address();
        addr.setProvince("广东");
        addr.setCity("深圳");
        return addr;
    }

    //集合
    @RequestMapping("/listAddr")
    public List
listAddr() { List
list = new ArrayList<>(); Address addr = new Address(); addr.setProvince("广东"); addr.setCity("深圳"); Address addr1 = new Address(); addr1.setProvince("南京"); addr1.setCity("江苏"); Address add2 = new Address(); add2.setProvince("青岛"); add2.setCity("上海"); list.add(addr); list.add(addr1); list.add(add2); return list; } }

响应数据web_第3张图片响应数据web_第4张图片响应数据web_第5张图片响应数据web_第6张图片

 

package com.example.demo.controller.poio;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

//字符串格式返回
/*
@RestController
public class ResponseBody {
    @RequestMapping("/hello")
    public String hello() {
        String st = "hello world ~";
        System.out.println(st);
        return st;
    }

    //json 格式返回
    @RequestMapping("/getaddr")
    public Address getaddr() {
        Address addr = new Address();
        addr.setProvince("广东");
        addr.setCity("深圳");
        return addr;
    }

    //集合
    @RequestMapping("/listAddr")
    public List
listAddr() { List
list = new ArrayList<>(); Address addr = new Address(); addr.setProvince("广东"); addr.setCity("深圳"); Address addr1 = new Address(); addr1.setProvince("南京"); addr1.setCity("江苏"); Address add2 = new Address(); add2.setProvince("青岛"); add2.setCity("上海"); list.add(addr); list.add(addr1); list.add(add2); return list; } } */ @RestController public class ResponseBody { @RequestMapping("/hello") public Result hello() { String st = "hello world ~"; System.out.println(st); // return new Result(1,"success",st); return Result.success("hello world"); } //json 格式返回 @RequestMapping("/getaddr") public Result getaddr() { Address addr = new Address(); addr.setProvince("广东"); addr.setCity("深圳"); return Result.success(addr); } //集合 @RequestMapping("/listAddr") public Result listAddr() { List
list = new ArrayList<>(); Address addr = new Address(); addr.setProvince("广东"); addr.setCity("深圳"); Address addr1 = new Address(); addr1.setProvince("南京"); addr1.setCity("江苏"); Address add2 = new Address(); add2.setProvince("青岛"); add2.setCity("上海"); list.add(addr); list.add(addr1); list.add(add2); return Result.success(list); } }

package com.example.demo.controller.poio;

public class Result {
    private Integer code;  //成功1,失败1
    private String msg ;  //提供信息
    private Object data;  //数据

    public Result() {
    }

    public Result(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    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 success(String msg){
        return new Result(0,"success",null);
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}

 

你可能感兴趣的:(windows)