SSM學習記錄(七)——使用JSON進行前後臺數據傳輸及Postman測試

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傳輸數據的知識點遠不止這一點

本文僅給出了一個最簡單的實例

更深層次的內容實在不敢說掌握了

希望大家能夠自己去了解



謝謝~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章