首页 > 其他分享 >第九篇 - SpringBoot统一结果封装Json数据

第九篇 - SpringBoot统一结果封装Json数据

时间:2023-07-06 15:03:48浏览次数:42  
标签:SpringBoot 第九篇 String Json User password com public user

前面几节学习了SpringBoot和Vue的结合,以及Vue跳转到另一个Vue页面。这节学习SpringBoot controller返回Json数据格式封装。

参考链接:https://zhuanlan.zhihu.com/p/347233348

第一步:在entity文件夹下新建一个ResultVo类。

 ResultVo.java

package com.example.demo.entity;

public class ResultVo {
    //相应码
    private Integer code;
    //信息
    private String message;
    //返回数据
    private Object data;

    public ResultVo(Integer Code, String Message, Object Data){
        this.code = Code;
        this.message = Message;
        this.data = Data;
    }
}
View Code

 

第二步:修改User.java

package com.example.demo.entity;

public class User {
    public String user_account;
    public String user_password;

    public User(String User_account, String User_password){
        this.user_account = User_account;
        this.user_password = User_password;
    }
}
View Code

 

第三步:新建LoginVo.java

package com.example.demo.entity;

import java.io.Serializable;

public class LoginVo implements Serializable {
    private  User user;
    public LoginVo(User user_obj){
        this.user = user_obj;
    }
}
View Code

 

第四步:修改UserController.java的/login

package com.example.demo.controller;

import com.example.demo.entity.LoginVo;
import com.example.demo.entity.User;
import com.example.demo.entity.ResultVo;
import com.example.demo.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "http://localhost:8080")
public class UserController {
    @Autowired
    private UserServiceImpl userServer;

    @GetMapping("/users")
    public List<User> getUserList()
    {
        return userServer.getUserList();
    }

    @PostMapping("/login")
    @ResponseBody
    public ResultVo loginStatus(HttpServletRequest req)
    {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username);
        System.out.println(password);
        List<User> userlist = userServer.getUserList();
        for (User us: userlist){
            if (us.user_account.equals(username) && us.user_password.equals(password)){
                return new ResultVo(200, "登录成功", new LoginVo(us));
            }
        }
        return new ResultVo(201, "用户名/密码错误", "");
    }
}
View Code

 

第五步:用postman测试接口

 /user接口可以正常访问,在来试试我们刚修改的/login

 报错了,后端显示的错误是Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]

参考链接:https://stackoverflow.com/questions/28466207/could-not-find-acceptable-representation-using-spring-boot-starter-web

原因在于没有给ResultVo写get函数,因此修改ResultVo.java

package com.example.demo.entity;

public class ResultVo {
    //相应码
    private Integer code;
    //信息
    private String message;
    //返回数据
    private Object data;

    public Integer getCode(){
        return this.code;
    }

    public String getMessage(){
        return this.message;
    }

    public Object getData(){
        return this.data;
    }

    public ResultVo(Integer Code, String Message, Object Data){
        this.code = Code;
        this.message = Message;
        this.data = Data;
    }

}
View Code

 

再次用postman测试

 可以看到成功输出我们想要的报错信息了,然后试试用户名密码正确的情况

 现在应该可以猜到为什么报错了吗?没错,因为LoginVo.java和User.java里面也要添加get函数啊

LoginVo.java

package com.example.demo.entity;

import java.io.Serializable;

public class LoginVo implements Serializable {
    private  User user;

    public User getUser(){
        return this.user;
    }
    public LoginVo(User user_obj){
        this.user = user_obj;
    }
}
View Code

 

User.java

package com.example.demo.entity;

public class User {
    public String user_account;
    public String user_password;

    public String getUser_account(){
        return this.user_account;
    }

    public String getUser_password(){
        return this.user_password;
    }

    public User(String User_account, String User_password){
        this.user_account = User_account;
        this.user_password = User_password;
    }
}
View Code

 

再来试试,测试登录成功

 是不是就大功告成了。

标签:SpringBoot,第九篇,String,Json,User,password,com,public,user
From: https://www.cnblogs.com/smart-zihan/p/17532142.html

相关文章

  • 2023年7月最新全国省市区县和乡镇街道行政区划矢量边界坐标经纬度地图数据 shp geojso
    发现个可以免费下载全国 geojson 数据的网站,推荐一下。支持全国、省级、市级、区/县级、街道/乡镇级以及各级的联动数据,支持导入矢量地图渲染框架中使用,例如:D3、Echarts等geojson数据下载地址:https://geojson.hxkj.vip该项目github地址:https://github.com/TangSY/echarts-m......
  • scala class、Map、List 转换成Json(Gson、json4s、JSONUtil)
    实例代码importcn.hutool.json.JSONUtilimportcom.google.gson.GsonobjectEntitytoJsonTest{defmain(args:Array[String]):Unit={valgson=newGsonvalpeople=JJ("gl",12,List("basketball","baseball"),......
  • package.json指南
    一、属性name定义项目的名称,不能以"."和"_"开头,不能包含大写字母version定义项目的版本号,格式为:大版本号.次版本号.修订号description项目的描述二、配置dependencies生产环境的依赖包如果不使用脱字符(^),安装的版本号固定;如果使用,则能安装当前大版本的最新版本,在......
  • springboot starter使用
    实现自定义starterpom.xml依赖<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http:......
  • SpringBoot笔记:SpringBoot启动参数配置
    springboot启动参数/usr/local/jdk/jdk1.8.0_261/bin/java-jar-server\ ##服务模式,linux默认是server模式,window默认是client参数-XX:+HeapDumpOnOutOfMemoryError\ ##当OOM发生时自动生成HeapDump文件-XX:HeapDumpPath=/usr/local/springboot_......
  • Websocket+SpringBoot实现简单在线聊天(包含前后端代码)
    1、样式展示登录界面(用户名自己取,密码是111,可在前端文件中改,因为做的比较简单,没有把用户做数据库相关的,所以直接在前端固定了密码是111)聊天界面2、代码展示前端<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatibl......
  • springboot修改配置
    springboot修改配置在resources下的Application.properties端口:#服务器端口配置server.port=80logo:#修改bannerspring.main.banner-mode=off 图片:#配置图像spring.banner.image.location=de.jpg日志#日志logging.level.root=error查看.properties可以去spring......
  • 4. SpringBoot整合mybatis
    1.回顾Spring整合Mybatis​Spring​整合Mybatis​需要定义很多配置类​SpringConfig​配置类导入JdbcConfig​配置类导入MybatisConfig​配置类@Configuration@ComponentScan("com.itheima")@PropertySource("classpath:jdbc.properties")@Import({Jdbc......
  • springboot 加载自定义的属性配置文件 或者xml文件
    1、properties user.propertiesname=zhangshanage=18  2、xml Pen1.xml<?xmlversion="1.0"encoding="utf-8"?><!DOCTYPEpropertiesSYSTEM"http://java.sun.com/dtd/properties.dtd"><properties><......
  • VUE 2项目使用vue-json-excel导出数据
    记录一下后端返回的json数据转成excel导出这里外面使用的是vue-json-excel1.安装包npminstallvue-json-excel2.组件中使用<download-excelclass="btnbtn-default":data="json_data":fields="json_fields"worksheet="MyWorksheet"name=&......