写在前面
作为一个Android开发者为什么要撸一个网络测试服务呢?
试想一下,当你想自己封装下网络请求库,埋头撸出来了,也不知道有没有问题,
反正就是撸出来了,有没有问题不知道,想要测试要不在网上找相关的开放api去测试,
但是哪有完全符合自己预期的开放api,为了解决这个问题,动手自己撸一个测试服务吧。
正文开始
服务项目相关
- Java环境,作为Android开发者,这个已经具备。
- 数据库,这个在测试服务非必须,不做展开。
- IDE,这里采用IDEA,可以用试用版,完全足够了。
开撸
- SpringBoot项目建立,选择JavaWeb项目相关,数据库相关不需要(mybatis,jpa)都不需要。
- 生成的application如下,SpringBootApplication忽略数据库相关配置:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class SimpleApplication {
public static void main(String[] args) {
SpringApplication.run(SimpleApplication.class, args);
}
}
- 在application.properties项目配置
#项目配置相关
#访问路径
server.servlet.context-path=/simple
#端口号
server.port=8085
#编码
server.tomcat.uri-encoding=utf-8
# 上传文件大小限制修改
spring.servlet.multipart.max-file-size=1000MB
spring.servlet.multipart.max-request-size=10000MB
- 新建Controller如下:
@RestController
public class ControllerTest {
}
- 新建返回类如下,注意要使用@Data需要引入lombok,安装lombok插件:
@Data
public class ResultVO<T> {
private Integer code;
private String message;
private T body;
}
- 在第4步的Controller新建Get请求,有两个参数id和code,主要打印出两个请求参数,并构建返回:
@GetMapping("/info")
public ResultVO<String> doGet(String id, String code) {
System.out.println("id:" + id + "\ncode:" + code);
ResultVO<String> resultVO = new ResultVO<String>();
resultVO.setCode(200);
resultVO.setMessage("Get请求成功");
resultVO.setBody("id=" + id + "---" + "code=" + code);
return resultVO;
}
- Post表单请求,跟get类似,但是需要两个header session和deviceId
@PostMapping("/login")
public ResultVO<String> doPost(@RequestHeader("session") String session,
@RequestHeader("deviceId")String deviceId,
@RequestParam("userName") String userName, @RequestParam("password") String password) throws IOException {
System.out.println("session:"+session);
System.out.println("deviceId:"+deviceId);
System.out.println("userName:" + userName + "\npassword:" + password);
ResultVO<String> resultVO = new ResultVO<String>();
resultVO.setCode(200);
if (null == userName) {
throw new IOException("123");
}
resultVO.setMessage("PostParas请求成功");
resultVO.setBody("userName=" + userName + "---" + "password=" + password);
return resultVO;
}
- Post Json请求,需要一个请求实体类,实体类和方法如下:
@Data
public class LoginBean {
private String userName;
private String password;
}
@PostMapping("/login1")
public ResultVO<LoginBean> doPost1(@RequestBody LoginBean loginBean) {
System.out.println("userName:" + loginBean.getUserName());
System.out.println("password:" + loginBean.getPassword());
ResultVO<LoginBean> resultVO = new ResultVO<LoginBean>();
resultVO.setCode(200);
resultVO.setMessage("PostJson请求成功");
resultVO.setBody(loginBean);
return resultVO;
}
- 上传文件请求,进行文件存储并返回路径,存储在用户下的upload文件夹,将用户名改成自己的mac用户(Windows环境将存储路径进行修改)
@PostMapping("/uploadFile")
public ResultVO<String> uploadFile(@RequestParam("file") MultipartFile file, Model model, HttpServletRequest request) {
if (file == null) {
System.out.println("文件为空");
ResultVO resultVO = new ResultVO();
resultVO.setCode(500);
resultVO.setMessage("上传文件为空");
return resultVO;
}
String fileName = file.getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf("."));
String filePath = "/Users/用户名/upload/";
fileName = UUID.randomUUID() + suffixName;
File dest = new File(filePath + fileName);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
try {
file.transferTo(dest);
} catch (IOException e) {
e.printStackTrace();
}
String fullFileName = filePath + fileName;
model.addAttribute("fullFileName" + fullFileName);
ResultVO resultVO = new ResultVO();
resultVO.setCode(200);
resultVO.setMessage("上传成功");
resultVO.setBody("成功:" + fullFileName);
return resultVO;
}
- 上传多文件,和上传文件类似
@PostMapping("/uploadFiles")
public ResultVO<List<String>> uploadFile(@RequestParam("files") MultipartFile[] files, Model model, HttpServletRequest request) {
if (files == null) {
System.out.println("多文件为空");
ResultVO resultVO = new ResultVO();
resultVO.setCode(500);
resultVO.setMessage("上传的文件为空");
return resultVO;
}
List<String> fullNames = new ArrayList<>(files.length);
for (MultipartFile file : files) {
String fileName = file.getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf("."));
String filePath = "/Users/qms/upload/";
fileName = UUID.randomUUID() + suffixName;
File dest = new File(filePath + fileName);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
try {
file.transferTo(dest);
} catch (IOException e) {
e.printStackTrace();
}
String fullFileName = filePath + fileName;
model.addAttribute("fullFileName:" + fullFileName);
fullNames.add(fullFileName);
}
ResultVO resultVO = new ResultVO();
resultVO.setCode(200);
resultVO.setMessage("上传成功");
resultVO.setBody(fullNames);
return resultVO;
}
- 参数文件混传,需要一个上传的Bean,Bean及方法如下:
@PostMapping("/uploadAndLogin")
public ResultVO<String> uploadAndLogin(UploadBean bean){
System.out.println("userName:" + bean.getUserName());
System.out.println("password:" + bean.getPassword());
String fileName = bean.getFile().getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf("."));
String filePath = "/Users/qms/upload/";
fileName = UUID.randomUUID() + suffixName;
File dest = new File(filePath + fileName);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
try {
bean.getFile().transferTo(dest);
} catch (IOException e) {
e.printStackTrace();
}
String fullFileName = filePath + fileName;
ResultVO resultVO = new ResultVO();
resultVO.setCode(200);
resultVO.setMessage("上传成功");
resultVO.setBody("成功:"+fullFileName);
return resultVO;
}
撸方法到此结束
到此,启动SpringBoot项目可以请求本机服务了。
Https请求
此处采用P12证书
- 证书生成:
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650
执行之后一步步往下走即可,别名为tomcat,可以自己修改,记住自己输入的密码,配置需要用到。
- 将生成的证书配置到SpringBoot项目的resources目录下keystore.p12
- 在application.properties配置项目:
#https相关的配置
server.ssl.enabled=true
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=密码
server.ssl.key-store-type=PKCS12
server.ssl.key-alias=tomcat
到此Https配置结束
请求转发,代码配置如下:
@Configuration
public class HttpsConfig {
@Bean
TomcatServletWebServerFactory tomcatServletWebServerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint constraint = new SecurityConstraint();
constraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
//添加 防止post请求转发出错
collection.addMethod(DEFAULT_PROTOCOL);
constraint.addCollection(collection);
context.addConstraint(constraint);
}
};
factory.addAdditionalTomcatConnectors(createTomcatConnector());
return factory;
}
private Connector createTomcatConnector() {
Connector connector = new
Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(8085);
return connector;
}
}
配置结束
请求8080的http请求即可转发到8085的https请求。
源码
链接:源码
写在最后
到此结束,我们的测试服务已经写Ok了,其实也没多少内容,一个Controller搞定,
可以愉快地测试我们的网络服务了。