SSM学习记录(七)——使用JSON进行前后台数据传输及Postman测试

9 篇文章 0 订阅
8 篇文章 0 订阅

2018.5.8

仅为个人理解 不足之处欢迎指正~


在之前的测试中,除了事务管理所用的“一次增加两个用户”操作是仅用指令实现

其他的测试都编写了相应的JSP页面 使用EL表达式进行前后台数据的交互

在实际操作中 大多情况下前后台数据传输是使用JSON进行的

这次进行一个使用JSON传输数据并使用POSTMAN测试的简单测试


第一步:导入所需包

 本项目使用Maven搭建

加入的包为:

 <dependency>
    	<groupId>com.fasterxml.jackson.core</groupId>
    	<artifactId>jackson-core</artifactId>
    	<version>2.8.9</version>
    </dependency>
    <dependency>
    	<groupId>com.fasterxml.jackson.core</groupId>
    	<artifactId>jackson-databind</artifactId>
    	<version>2.8.9</version>
    </dependency>
    <dependency>
    	<groupId>com.fasterxml.jackson.core</groupId>
    	<artifactId>jackson-annotations</artifactId>
    	<version>2.8.9</version>
    </dependency>
 

本项目目前使用的所有包为:



第二步:对SpringMVC的配置文件进行修改

需要在声明中加入:



并且开启:



本文使用的springContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:annotation-config/>
<context:component-scan base-package="controller" />
<mvc:annotation-driven />
    <!-- 视图页面配置 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <property name="prefix" value="/WEB-INF/views/" />  
        <property name="suffix" value=".jsp" />  
    </bean>
    
    <!-- 用于将对象转换为 JSON  -->

    <bean id="stringConverter"
          class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/plain;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    <!-- api -->
    <bean id="jsonConverter"
          class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="stringConverter" />
                <ref bean="jsonConverter" />
            </list>
        </property>
    </bean>
</beans>


第三步:编写Controller层

拦截 “jsontest”指令

传入数据userName  然后根据传入的userName找到一个User  然后将整个User和一个状态码一起返回


另:

ResultData为自己定义的类 

因为用户数据交互 所以通常放在vo包中 而不是pojo包中

package vo;

public class ResultData 
{
	Integer status;
	Object data;
	
	public ResultData(Integer status)
	{
		this.status=status;
	}
	
	public ResultData(Integer status,Object datas)
	{
		this.status=status;
		this.data=datas;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}
	
}

内含两个类型的数据:


status一般用于返回一个与前台约定好的状态码 比如-1为没有登录 -2为没有权限 -3为传值错误等等

Object类型的data可以用于任何数据的封装


第四步:使用PostMan测试


Header:


点击Send:


获取数据



总结:

使用JSON传输数据的知识点远不止这一点

本文仅给出了一个最简单的实例

更深层次的内容实在不敢说掌握了

希望大家能够自己去了解



谢谢~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值