记录springboot基本搭建以及作为接口测试的简单使用

Eclipse创建springboot项目我就不说了,我总结我自己搭建的过程
一:搭建springboot框架
1:https://start.spring.io/ 网站下载springboot框架最原始代码
记录springboot基本搭建以及作为接口测试的简单使用
2:解压源码,打开eclipse导入 (选择的是maven项目导入,前提本机安装了maven)
记录springboot基本搭建以及作为接口测试的简单使用
记录springboot基本搭建以及作为接口测试的简单使用
即可
导入后的目录
记录springboot基本搭建以及作为接口测试的简单使用
3:简单运行,编写一个测试方法
创建一个class文件 写一个测试方法
记录springboot基本搭建以及作为接口测试的简单使用

@RestController
@RequestMapping("/test")
public class HelloController {

    @RequestMapping("/hello")
	public String index(){
		return "hello";
	}
}

这时注解报错不着急,打开pom文件
加入

	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-web</artifactId>
	</dependency>

这时根据错误引入包即可
4:运行
springboot框架不像其他项目要加载到tomcat才能运行,springboot框架本身集成了Tomcat
先配置端口
记录springboot基本搭建以及作为接口测试的简单使用
加入
#端口号
server.port=8999
application.properties中文会出现乱码:
记录springboot基本搭建以及作为接口测试的简单使用
解决的办法:window–>perferences–>General(常规设置)–>content types–>Text–>java properties file
设置成UTF-8即可
打开DemoApplication 有个main方法,右键 run as -->java Applicant运行即可启动
记录springboot基本搭建以及作为接口测试的简单使用
浏览器打开 输入:http://127.0.0.1:9999/test/hello
记录springboot基本搭建以及作为接口测试的简单使用
二:编写接口调用测试 httpClient
1:在上面springboot框架的基础上先引入需要的httpClient jar包

	<!-- 引入httpClient start-->
	<dependency>
		<groupId>commons-httpclient</groupId>
		<artifactId>commons-httpclient</artifactId>
		<version>3.1</version>
	</dependency>
	<dependency>
		<groupId>org.apache.httpcomponents</groupId>
		<artifactId>httpclient</artifactId>
		<version>4.3.1</version>
	</dependency>
	<!-- 引入httpClient end-->

如果你还要获取接口的内容再转化成json,则再加入json的jar包即可

		<!--json包 和json依赖包ezmorph start -->
		 <dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<version>2.4</version>
			<classifier>jdk15</classifier>
		</dependency> 
		 <dependency>
			<groupId>net.sf.ezmorph</groupId>
			<artifactId>ezmorph</artifactId>
			<version>1.0.6</version>
		</dependency> 
		<!--json包 和json依赖包ezmorph end -->

如果pom文件加入jar包报错,则baidu即可,手动添加jar包到本地仓库(有空我再写命令)
2:编写接口调用方法
记录springboot基本搭建以及作为接口测试的简单使用
InterfaceController 内容与helloController 一致,不过添加一个方法,调用接口方法

 /**
     * 功能:获取用户信息列表接口
     * @param username 用户名 password 密码
     * @return
     */
    @RequestMapping("testOne")
	public String getUserList(HttpServletRequest request,
			HttpServletResponse resp) {
		String url="http://127.0.0.1:8077/demo/lalala";//接口调用的地址
		String username = "啦啦啦";//参数
		String password = "666666";//参数
		String result = interfaceService.postHttp(url,username,password);//postHttp 请求的公共方法
		System.out.println(result);//打印结果
		JSONObject jsonObject=JSONObject.fromObject(result);//将结果转化成json
		String message=jsonObject.getString("message");
		System.out.println(message);
		Object entity=jsonObject.get("entity");//list
		System.out.println(entity);
		if(entity!=null){
			JSONArray rhousecollectarray = JSONArray.fromObject(entity.toString());//json数组  
			if(rhousecollectarray.size()>0){
				 for(int i=0;i<rhousecollectarray.size();i++){
					 // 遍历 jsonarray 数组,把每一个对象转成 json 对象  
					 JSONObject rhousecollectObject = rhousecollectarray.getJSONObject(i);  
					 System.out.println(rhousecollectObject.get("username"));
				 }
			}
		}
		return result;
	}

message 和entity 必须与接口返回的名称一致 具体根据自己调用的接口改即可
InterfaceService 中编写httpClient接口请求通用方法 我只写了一种 post缓存流的方式读取

@Service
public class InterfaceService {
	/**
	 * post方式缓冲流方式
	 * @param url
	 * @param username
	 * @param password
	 * @author 某某
	 * @return
	 */
	public String postHttp(String url,String username,String password) {
		String responseMsg = "";
		HttpClient httpClient = new HttpClient();
		httpClient.getParams().setContentCharset("GBK");
		PostMethod postMethod = new PostMethod(url);
		postMethod.addParameter("username", username);//添加请求参数
		postMethod.addParameter("password", password);//添加请求参数
		try {
			httpClient.executeMethod(postMethod);
			ByteArrayOutputStream out = new ByteArrayOutputStream();//创建一个32字节(默认大小)的缓冲区
			InputStream in = postMethod.getResponseBodyAsStream();//缓冲流
			int len = 0;
			byte[] buf = new byte[1024];//设置每次读取1024个字节
			while((len=in.read(buf))!=-1){//遍历返回的总字节(例如总共2048字节 则循环两次结束)
				out.write(buf, 0, len);
			}
			responseMsg = out.toString("UTF-8");//返回缓冲流结果
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			postMethod.releaseConnection(); // 释放连接
		}
		   return responseMsg;
	}
}

调用接口方法就写完了
3:运行该方法
跟上面运行一样,运行起来后,在浏览器中输入
http://127.0.0.1:9999/hello/testOne
即可看到获取接口的值了,用于接口测试真的是再方便不过了,后续再更新springboot的知识