先导入jakeson的包
| <dependencies> |
| |
| <dependency> |
| <groupId>com.fasterxml.jackson.core</groupId> |
| <artifactId>jackson-databind</artifactId> |
| <version>2.13.1</version> |
| </dependency> |
| <dependency> |
| <groupId>org.projectlombok</groupId> |
| <artifactId>lombok</artifactId> |
| <version>1.18.10</version> |
| </dependency> |
| </dependencies> |
配置web.xml文件
| <?xml version="1.0" encoding="UTF-8"?> |
| <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" |
| version="4.0"> |
| |
| <servlet> |
| <servlet-name>SpringMVC</servlet-name> |
| <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> |
| |
| <init-param> |
| <param-name>contextConfigLocation</param-name> |
| <param-value>classpath:springmvc-servlet.xml</param-value> |
| </init-param> |
| |
| <load-on-startup>1</load-on-startup> |
| </servlet> |
| |
| |
| <servlet-mapping> |
| <servlet-name>SpringMVC</servlet-name> |
| <url-pattern>/</url-pattern> |
| </servlet-mapping> |
| |
| <filter> |
| <filter-name>encoding</filter-name> |
| <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> |
| <init-param> |
| <param-name>encoding</param-name> |
| <param-value>utf-8</param-value> |
| </init-param> |
| </filter> |
| <filter-mapping> |
| <filter-name>encoding</filter-name> |
| <url-pattern>/</url-pattern> |
| </filter-mapping> |
| |
| </web-app> |
配置springmvc-servlet.xml
| <?xml version="1.0" encoding="UTF-8"?> |
| <beans xmlns="http://www.springframework.org/schema/beans" |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xmlns:context="http://www.springframework.org/schema/context" |
| xmlns:mvc="http://www.springframework.org/schema/mvc" |
| xsi:schemaLocation="http://www.springframework.org/schema/beans |
| http://www.springframework.org/schema/beans/spring-beans.xsd |
| http://www.springframework.org/schema/context |
| https://www.springframework.org/schema/context/spring-context.xsd |
| http://www.springframework.org/schema/mvc |
| https://www.springframework.org/schema/mvc/spring-mvc.xsd"> |
| |
| |
| <context:component-scan base-package="com.Google.controller"/> |
| |
| <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" |
| id="internalResourceViewResolver"> |
| |
| <property name="prefix" value="/WEB-INF/jsp/" /> |
| |
| <property name="suffix" value=".jsp" /> |
| </bean> |
| |
| </beans> |
编写实体类
| @Data |
| @AllArgsConstructor |
| @NoArgsConstructor |
| public class User { |
| private String name; |
| private int age; |
| private String Sex; |
| } |
编写Controller,传递一个对象
| package com.Google.controller; |
| |
| import com.Google.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 jsonController { |
| @RequestMapping("/j1") |
| |
| public String json1() throws JsonProcessingException { |
| |
| ObjectMapper mapper = new ObjectMapper(); |
| User user = new User("Spring", 2, "女"); |
| String str = mapper.writeValueAsString(user); |
| return str; |
| } |
| } |
后端一般就是传字符串给前端,所以以后一般直接使用RestController
这里没有配置传输JSON乱码问题
可以使用RequestMapping
| |
| @RequestMapping(value = "/json1",produces = "application/json;charset=utf-8") |
最好就是同一处理(在spring中处理)
| |
| <mvc:annotation-driven> |
| <mvc:message-converters register-defaults="true"> |
| <bean class="org.springframework.http.converter.StringHttpMessageConverter"> |
| <constructor-arg value="UTF-8"/> |
| </bean> |
| <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> |
| <property name="objectMapper"> |
| <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"> |
| <property name="failOnEmptyBeans" value="false"/> |
| </bean> |
| </property> |
| </bean> |
| </mvc:message-converters> |
| </mvc:annotation-driven> |
传递一个数组
| |
| @RequestMapping("/j2") |
| public String json2() throws JsonProcessingException { |
| ObjectMapper mapper = new ObjectMapper(); |
| List<User> users = new ArrayList<>(); |
| User user = new User("Spring", 2, "女"); |
| User user1 = new User("Spring", 2, "女"); |
| User user2 = new User("Spring", 2, "女"); |
| User user3 = new User("Spring", 2, "女"); |
| users.add(user); |
| users.add(user1); |
| users.add(user2); |
| users.add(user3); |
| return mapper.writeValueAsString(users); |
| } |
传递一个时间对象
| |
| @RequestMapping("/j3") |
| public String json3() throws JsonProcessingException { |
| Date date = new Date(); |
| return new ObjectMapper().writeValueAsString(date); |
| } |
时间对象返回的结果是一个时间戳

我们需要更改一下它的格式
1、使用java纯代码
| |
| @RequestMapping("/j3") |
| public String json3() throws JsonProcessingException { |
| Date date = new Date(); |
| SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss"); |
| String format = dateFormat.format(date); |
| return new ObjectMapper().writeValueAsString(format); |
| } |
结果

2、使用jackson取消timestamps形式 , 自定义时间格式
| @RequestMapping("/j3") |
| public String json3() throws JsonProcessingException { |
| ObjectMapper mapper = new ObjectMapper(); |
| |
| mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); |
| Date date = new Date(); |
| |
| SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss"); |
| mapper.setDateFormat(dateFormat); |
| return mapper.writeValueAsString(date); |
| } |
结果

可以将重复使用的代码包装成一个类,来实现代码的复用(编写这类代码,思想很重要啊)
| public class JsonUtils { |
| |
| public static String getJson(Object object) { |
| return getJson(object,"yyyy-MM-dd HH:mm:ss"); |
| } |
| |
| public static String getJson(Object object,String dateFormat) { |
| ObjectMapper mapper = new ObjectMapper(); |
| |
| mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); |
| |
| SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); |
| |
| mapper.setDateFormat(sdf); |
| try { |
| return mapper.writeValueAsString(object); |
| } catch (JsonProcessingException e) { |
| e.printStackTrace(); |
| } |
| return null; |
| } |
| } |
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术