1.pom.xml导依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0</version>
</dependency>
2.添加框架支持web
3.新建一个lib文件把依赖导进去即可,不导会报错!
4.乱码问题
第一种
@RequestMapping(value = "/jsdn1",produces = "application/json;charset=utf-8")
第二种(建议使用)
<?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.bubbles.controller"/>
<!-- JSDN乱码问题配置-->
<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>
<!--视图解析器:DispatcherServlet给他的ModelAndView-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!--前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀-->
<property name="suffix" value=".jsp"/>
</bean>
5.创建实体类User
package com.bubbles.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
//导入lombok
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int id;
private String sex;
}
6.UserController类
package com.bubbles.controller;
import com.bubbles.pojo.User;
import com.bubbles.utils.Jsonutils;
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 javax.xml.ws.RespectBinding;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
//@Controller
@RestController
public class UserController {
@RequestMapping("/jsdn1")
//@ResponseBody //加上ResponseBody他就不会走视图解析器,会返回一个字符串
public String uesr() throws JsonProcessingException {
//是不是导入jackson包,他里面有个对象叫ObjectMapper
ObjectMapper mapper = new ObjectMapper ();
//创建一个对象
User user = new User ("bubbles1号",3,"男");
String str = mapper.writeValueAsString (user); //user变成字符串
return str;
}
@RequestMapping("/jsdn2")
public String uesr2() throws JsonProcessingException {
//创建一个对象
List<User> userList = new ArrayList<User> ();
User user1 = new User ("bubbles1号",3,"男");
User user2 = new User ("bubbles2号",3,"男");
User user3 = new User ("bubbles3号",3,"男");
User user4 = new User ("bubbles4号",3,"男");
User user5 = new User ("bubbles5号",3,"男");
userList.add (user1);
userList.add (user2);
userList.add (user3);
userList.add (user4);
userList.add (user5);
return Jsonutils.getjsdnt (userList); //new ObjetMapper().writeValueAsString (userList);
}
@RequestMapping("/jsdn3")
public String uesr3() throws JsonProcessingException {
//是不是导入jackson包,他里面有个对象叫ObjectMapper
Date date = new Date ();
//ObjectMapper,时间解析后的默认格式为:Timestamp,时间戳
return Jsonutils.getjsdnt (date,"yyyy-MM-dd HH:mm:ss"); //new ObjetMapper().writeValueAsString (userList);
}
}
7.Jsonutils类
package com.bubbles.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.text.SimpleDateFormat;
public class Jsonutils {
public static String getjsdnt(Object object) {
return getjsdnt (object,"yyyy-MM-dd HH:mm:ss");
}
public static String getjsdnt(Object object,String sdf){
//是不是导入jackson包,他里面有个对象叫ObjectMapper
ObjectMapper mapper = new ObjectMapper ();
//使用ObjectMapper来格式化输出
mapper.configure (SerializationFeature.WRITE_DATES_AS_TIMESTAMPS ,false);
//自定义日期的格式
SimpleDateFormat sf =new SimpleDateFormat (sdf);
mapper.setDateFormat (sf);
try {
return mapper.writeValueAsString (object);
} catch (JsonProcessingException e) {
e.printStackTrace ();
}
return null;
}
}
8.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">
<!-- 1.注册servlet-->
<servlet>
<servlet-name>Springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 通过初始化参数指定SpringMVC配置文件的位置,进行关联-->
<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>
<!--所有请求都会被springmvc拦截-->
<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>
9.输出结果