赞
踩
- package com.example.springbootjwtback.UserController;
-
- import com.example.springbootjwtback.entity.ResponseResult;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.*;
-
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * @program: springboot-jwt-back
- * @description:
- * @author: wjl
- * @create: 2023-07-24 00:10
- **/
- @Controller
- @Slf4j
- @ResponseBody
- public class TestController {
-
- @GetMapping("testGet/{name}")
- public ResponseResult testGet(@PathVariable("name") String name){
- log.info(name);
- GetEntity entity = new GetEntity("后端返回name", "后端返回age");
- return new ResponseResult(ResponseResult.OK,entity);
- }
-
- @PostMapping("testPost")
- public ResponseResult testPost(@RequestBody PostEntity entity){
- log.info(entity.toString());
- List<PostEntity> list = new ArrayList<>();
- list.add(new PostEntity("2021-02-03","peter","山西省"));
- list.add(new PostEntity("2021-02-04","alise","河北省"));
- list.add(new PostEntity("2021-02-05","lisa","北京市"));
- return new ResponseResult(ResponseResult.OK,list);
- }
- }
-
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
- class GetEntity{
- private String name;
- private String age;
- }
-
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
- class PostEntity{
- private String date;
- private String name;
- private String address;
- }
-
- package com.example.springbootjwtback.entity;
-
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- /**
- * @program: springboot-jwt-back
- * @description: 返回实体
- * @author: wjl
- * @create: 2023-07-09 15:52
- **/
- @Data
- @NoArgsConstructor
- public class ResponseResult {
-
- /**
- * 表明该请求被成功地完成,所请求的资源发送到客户端
- */
- public static final Integer OK = 200;
- /**
- * 请求要求身份验证,常见对于需要登录而用户未登录的情况。
- */
- public static final Integer UNAUTHORIZED = 401;
- /**
- * 服务器拒绝请求,常见于机密信息或复制其它登录用户链接访问服务器的情况。
- */
- public static final Integer FORBIDDEN = 403;
- /**
- * 服务器无法取得所请求的网页,请求资源不存在。
- */
- public static final Integer NOT_FOUND = 404;
- /**
- * 服务器内部错误。
- */
- public static final Integer SERVER_ERROR = 500;
-
- private Integer code;
- private String msg = "";
- private Object data = new int[0];
-
-
- public ResponseResult(Integer code) {
- this.code = code;
- }
-
- public ResponseResult(Integer code, Object data) {
- this.code = code;
- this.data = data;
- }
-
- public ResponseResult(Integer code, String msg, Object data) {
- this.code = code;
- this.msg = msg;
- this.data = data;
- }
- }
@CrossOrigin(origins = {"*", "null"})// 解决跨域问题
npm install axios
-
- import axios from "axios";
- clicktest() {
- alert("come in");
- this.selfName = "更新后张三";
- this.student = { name: "更新后name", age: "更新后age" };
- axios({
- url: "http://localhost:8888/testGet/张三",
- method: "GET",
- }).then(res => {
- console.log(res.data);
- this.student = res.data.data;
- })
- },
- testsubmit(){
- alert("come in");
- axios({
- url: "http://localhost:8888/testPost",
- method: "POST",
- data: {
- date: "2022-02-02",
- name: "张三",
- address: "hebei"
- }
- }).then(res => {
- this.tableData = res.data.data
- })
- }
-
- @PostMapping("interpost")
- public ResponseResult interpost(@RequestBody GetEntity entity,@RequestHeader("token") String token){
- log.info(entity.toString());
- log.info("token:"+token);
- List<PostEntity> list = new ArrayList<>();
- list.add(new PostEntity("2021-02-03","peter","山西省"));
- list.add(new PostEntity("2021-02-04","alise","河北省"));
- list.add(new PostEntity("2021-02-05","lisa","北京市"));
- return new ResponseResult(ResponseResult.OK,list);
- }
-
- @PostMapping("interget")
- public ResponseResult interget(@RequestBody GetEntity entity,@RequestHeader("token") String token){
- log.info(entity.toString());
- log.info("token:"+token);
- List<PostEntity> list = new ArrayList<>();
- list.add(new PostEntity("2021-02-03","peter","山西省"));
- list.add(new PostEntity("2021-02-04","alise","河北省"));
- list.add(new PostEntity("2021-02-05","lisa","北京市"));
- return new ResponseResult(ResponseResult.OK,list);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。