81.【SpringMVC】(八)

简介: 81.【SpringMVC】

5.@RestController 注解

package Com.Jsxs.Controller;
import Com.Jsxs.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController   //这个注解直接不走视图解析器了,实现前后端分离
public class UserController {
    @RequestMapping("/j1")
    public String test() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
//        创建一个对象
        User user = new User("吉士先生",21,"男");
        String s = mapper.writeValueAsString(user);
        return s;
    }
}

6.Java对象转JSON字符串【对象】

ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(user);
package Com.Jsxs.Controller;
import Com.Jsxs.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@Controller   //这个注解直接不走视图解析器了,实现前后端分离
public class UserController {
    @RequestMapping("/j1")
    @ResponseBody
    public String test() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
//        创建一个对象
        User user = new User("吉士先生",21,"男");
        String s = mapper.writeValueAsString(user);
        return s;
    }
}

7.Java集合转JSON字符串【集合】

ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(userList);
package Com.Jsxs.Controller;
import Com.Jsxs.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController   //这个注解直接不走视图解析器了,实现前后端分离
public class UserController {
   @RequestMapping("/j2")
    public String test2() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        List<User> userList = new ArrayList<>();
//        创建一个对象
        User user = new User("吉士先生",21,"男");
        User user2 = new User("吉士先生2",21,"男");
        User user3 = new User("吉士先生3",21,"男");
        User user4 = new User("吉士先生4",21,"男");
        User user5 = new User("吉士先生5",21,"男");
        userList.add(user);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);
        userList.add(user5);
        String s = mapper.writeValueAsString(userList);
        return s;
    }
}

8.Java日期转JSON字符串【日期】

(1).输出的是时间戳
Date date = new Date();   输出的话会是一个 时间戳
date.toString();   输出的话会是一个 GMT
package Com.Jsxs.Controller;
import Com.Jsxs.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@RestController   //这个注解直接不走视图解析器了,实现前后端分离
public class UserController {
    @RequestMapping("/j3")
    public String test3() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
//        创建一个对象
        Date date = new Date();
//        自定义日期格式
        String s = mapper.writeValueAsString(date.toString());
        return s;
    }
}

(2).时间戳转换为自定义日期格式

把时间戳转换为自定义的日期格式

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.format(date);
package Com.Jsxs.Controller;
import Com.Jsxs.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@RestController   //这个注解直接不走视图解析器了,实现前后端分离
public class UserController {
    @RequestMapping("/j3")
    public String test3() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
//        创建一个对象
        Date date = new Date();
//        自定义日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = mapper.writeValueAsString(sdf.format(date));
        return s;
    }
}

(3).自定义日期格式+更改配置
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
package Com.Jsxs.Controller;
import Com.Jsxs.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@RestController   //这个注解直接不走视图解析器了,实现前后端分离
public class UserController {
    @RequestMapping("/j3")
    public String test3() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
//        创建一个对象
        Date date = new Date();
//        自定义日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        mapper.setDateFormat(sdf);
        String s = mapper.writeValueAsString(date);
        return s;
    }
}

(十四)、FastJson

1.什么是FastJson?

在前后端数据传输交互中,经常会遇到字符串(String)与json,XML等格式相互转换与解析,其中json以跨语言,跨前后端的优点在开发中被频繁使用,基本上可以说是标准的数据交换格式。fastjson 是一个java语言编写的高性能且功能完善的JSON库,它采用一种“假定有序快速匹配”的算法,把JSON Parse 的性能提升到了极致.它的接口简单易用,已经被广泛使用在缓存序列化,协议交互,Web输出等各种应用场景中。可以方便的实现Json对象与JavaBean对象的转换,实现JavaBEAN对象与json字符串的转换。

2.搭配环境

(1).导入依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.60</version>
</dependency>
(2).在Aftrical里面的lib目录添加jar包

3.FastJson实战演练

System.out.println("********Java 对象 转 JSON 字符串*********");
        String s = JSON.toJSONString(userList);
        System.out.println("Java 对象转换Json字符串为:"+userList);
        System.out.println("\n********JSON 字符串 转 Java 对象*********");
        User user1 = JSON.parseObject(JSON.toJSONString(user2),User.class);
        System.out.println("JSON 字符串转换Java对象为:"+user1);
        System.out.println("\n********Java 对象 转 JSON 对象*********");
        JSONObject o = (JSONObject) JSON.toJSON(user2);
        System.out.println("Java 对象转换为JSON对象后为:"+o.getString("name"));
        System.out.println("\n********JSON 对象 转 Java 对象*********");
        User user6 = JSON.toJavaObject(o, User.class);
        System.out.println("JSON 对象转换Java 对象后为:"+user6);
package Com.Jsxs.Controller;
import Com.Jsxs.pojo.User;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@RestController   //这个注解直接不走视图解析器了,实现前后端分离
public class UserController {
    @RequestMapping("/j4")
    public String test4(){
        List<User> userList = new ArrayList<>();
        User user = new User("吉士先生",21,"男");
        User user2 = new User("吉士先生2",21,"男");
        User user3 = new User("吉士先生3",21,"男");
        User user4 = new User("吉士先生4",21,"男");
        User user5 = new User("吉士先生5",21,"男");
        userList.add(user);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);
        userList.add(user5);
        System.out.println("********Java 对象 转 JSON 字符串*********");
        String s = JSON.toJSONString(userList);
        System.out.println("Java 对象转换Json字符串为:"+userList);
        System.out.println("\n********JSON 字符串 转 Java 对象*********");
        User user1 = JSON.parseObject(JSON.toJSONString(user2),User.class);
        System.out.println("JSON 字符串转换Java对象为:"+user1);
        System.out.println("\n********Java 对象 转 JSON 对象*********");
        JSONObject o = (JSONObject) JSON.toJSON(user2);
        System.out.println("Java 对象转换为JSON对象后为:"+o.getString("name"));
        System.out.println("\n********JSON 对象 转 Java 对象*********");
        User user6 = JSON.toJavaObject(o, User.class);
        System.out.println("JSON 对象转换Java 对象后为:"+user6);
        return s;
    }
}


相关文章
|
9天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
10565 29
|
9天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
2890 15
|
4天前
|
人工智能 安全 应用服务中间件
NGINX 爆出两处高危漏洞 可导致未经身份验证的远程代码执行
NGINX曝出两个高危漏洞(CVE-2026-123633和CVE-2026-123007),CVSS 9.2分,均属HTTP/2内存安全缺陷,无需认证即可远程执行代码。影响自2015年起的多个版本,静态站点亦受威胁。修复版本为开源版1.30.7/1.31.9。(239字)
1440 3
NGINX 爆出两处高危漏洞 可导致未经身份验证的远程代码执行
|
7天前
|
传感器 安全 区块链
Windows 电脑运行安卓应用教程:开源工具 ApkStart 双击 APK 即装、独立窗口运行
ApkStart 是一款开源Windows工具,让安卓APP像原生软件一样运行:双击APK即安装、生成真实图标、独立窗口启动、开始菜单可搜索。无需传统模拟器,内置QEMU运行时,支持数据互通、ARM兼容与命令行操作。(239字)
|
1天前
|
存储 安全 网络协议
模型列表里看不到新上线的模型?分清内置目录、端点探测与能力声明
在 DSH 中配置模型提供商时,最容易混淆的是三类不同的「模型信息来源」:**已安装目录**、**端点实时探测**和**使用者自行声明的能力**。内置提供商的探测结果来自已安装目录,即使 API 地址指向网关,也不会变成网关的实时列表;列表中出现某个模型 ID,也不代表端点确实支持相应的上下文窗口或图片输入。本文依次梳理三个配置路径、列表看似过时的原因、探测与真实请求的区别、凭据和网络的排查方式,以及安全边界。
428 95
|
12天前
|
人工智能 前端开发 API
阿里云Qwen4-Max 大模型详解:定位、特征、能力、适用场景与相对Qwen3-Max差异解析
2026年11月11日,阿里发布第四代旗舰模型Qwen4-Max预览版,首发千问AI平台Token Plan并接入千问办公、Qoder等入口。该版本聚焦知识理解、世界感知、编程与Agentic四项升级,主打复杂编程、专业办公、前端设计与专业软件操作等真实任务。预览期为11月11日10:00至11月25日24:00,仅支持Token Plan调用,到期后模型标识下线;限时2折、新用户再享5折,最低1折可体验。
|
12天前
|
JSON 前端开发 API
阿里云最新Qwen4-Max大模型介绍:模型定位、特征、能力、适用场景解析
2026 年 11 月 11 日,阿里发布第四代旗舰模型Qwen4-Max预览版,首发千问 AI 平台 Token Plan,并同步接入千问办公、Qoder 等入口。该版本聚焦知识理解、世界感知、编程与 Agentic 四项升级,主打复杂编程、专业办公、前端设计与专业软件操作。预览期至 11 月 25 日 24:00,到期后模型标识下线;限时 2 折、新用户再享 5 折,最低 1 折可体验。
|
8天前
|
人工智能 弹性计算 开发工具
零成本玩各大模型!DeepSeek Harness dsh-our-free-model 插件,13 个厂商模型直接用
总结一下:装这个插件,你不用注册、不用绑卡、不用填一个 Key,就能在 DeepSeek Harness 里用上 DeepSeek V4.1 Flash、Kimi K3 这一批前沿模型,而且是白嫖的、还不限量。三步搞定:下载插件,复制包含 `package.json` 的插件目录路径,在「添加插件」里粘贴路径安装并重启。无论是在阿里云 ECS 上通过命令行部署的 Web 版,还是本地的 Electron 桌面端,都能顺利跑通,快速搭建一套零成本的免费模型使用环境。
1526 1
|
25天前
|
Linux 开发工具 iOS开发
Codex保姆级教程(下载+安装+登录+使用,含所有版本,2026最新)
Codex是OpenAI推出的开源AI编程智能体,本地运行,可理解整个代码库,支持自然语言指令改代码、执行命令、跑测试、修Bug,兼容Win/macOS/Linux。(239字)
12867 60
|
11天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是DeepSeek AI开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、跑命令、处理表格、联网搜索等操作。免费开源,兼容Windows/macOS/Linux,提供桌面端与网页端,可灵活安装插件扩展能力。