Springmvc-json

maven+WEB  IDEA子目录下创建lib添加jar,  自由配置过滤器

<build>

<resources>

<resource>

<directory>src/main/java</directory>

<includes>

<include>**/*.xml</include>

</includes>

<filtering>false</filtering>

</resource>

<resource>

<directory>src/main/resources</directory>

<includes>

<include>**/*.xml</include>

</includes>

<filtering>false</filtering>

</resource>

</resources>

</build>

以上是提醒 , 正文从这里开始

这里使用jackson ,第一步导入包:

<dependencies>

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.9.8</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">

<!--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>

</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">

<!-- 自动扫描指定的包,下面所有注解类交给IOC容器管理 -->

    <context:component-scan base-package="com.it.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>

第四步 编写实体类

@org.springframework.stereotype.Controller

//@RestController 指定该类下所有方法返回值变成json ,可以不用再写@ResponseBody

//json就是js的字符串  , 用于与java对象的转换

public class Controller {

//手动指定utf-8防止乱码,此方法不够强大

@RequestMapping(value ="/json1",produces ="application/json;charset=utf-8")

@ResponseBody //指定方法返回类型变成json

    public String jackson1()throws JsonProcessingException {

ObjectMapper mapper =new ObjectMapper();

User user =new User("jackson哈哈",1,"json");

String str = mapper.writeValueAsString(user);

return str;

}

//测试多对象

@RequestMapping("/json2")

@ResponseBody

    public String jackson2()throws JsonProcessingException {

ObjectMapper mapper =new ObjectMapper();

User json1 =new User("1号",1,"json");

User json2 =new User("2号",1,"json");

User json3 =new User("3号",1,"json");

User json4 =new User("4号",1,"json");

List users =new ArrayList<>();

users.add(json1);

users.add(json2);

users.add(json3);

users.add(json4);

String str = mapper.writeValueAsString(users);

return str;

}

@RequestMapping("/json3")

public String jackson3()throws JsonProcessingException {

ObjectMapper mapper =new ObjectMapper();

Date date =new Date();

String str = mapper.writeValueAsString(date);

return str;

}


@RequestMapping("/json4")

@ResponseBody

public String jackson4()throws JsonProcessingException {

ObjectMapper mapper =new ObjectMapper();

//不使用时间差的方式

mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);

SimpleDateFormat sdf =new SimpleDateFormat("YYY-MM-dd HH:mm:ss");

Date date =new Date();

//指定日期格式

return mapper.writeValueAsString(sdf.format(date));

}

}

第五步 可以将其抽取成工具类

import java.text.SimpleDateFormat;

import java.util.Date;

public class DateUtils {

public static String getJson(Object object)throws JsonProcessingException {

return getJson(object,"yyyy-MM-dd HH:mm:ss");

}

public static String getJson(Object object,String dateFormat)throws JsonProcessingException {

ObjectMapper mapper =new ObjectMapper();

mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);

SimpleDateFormat sdf =new SimpleDateFormat(dateFormat);

ObjectMapper obj = mapper.setDateFormat(sdf);

return mapper.writeValueAsString(obj);

}

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容