POSTMAN were tested using the API program SPringMVC

1. Get API:

Controller level code into the splice equestMappin address corresponding to g corresponding to the API of the composition; wherein the root port and sometimes configuration file which has been set:


For example: This procedure is configured in the port application.propertities 8099; root path / api;


package com.redstar.sample.controller;

import com.redstar.sample.vo.AuthVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.redstar.sample.basic.Result;
import com.redstar.sample.dto.User;
import com.redstar.sample.service.UserService;
import com.redstar.sample.vo.UserServiceVo;

import lombok.extern.slf4j.Slf4j;

import java.util.UUID;

@Slf4j // 简单日志门面(Simple Logging Facade for Java),这里引入了lombok这个JAR包
@RestController // 定义controller类,返回的是数据类型,Controller返回的是web页面
@RequestMapping("user") // RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。
						// 用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径
public class SampleController {

	@Autowired // @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作
	UserService userService;


	/**
	 * 用户登录(mock)
	 * 
	 * @param loginAccount
	 * @return
	 */
	@RequestMapping("login")
	public Result login(@RequestBody User loginAccount) {
		System.out.println("login:" + loginAccount != null ? (loginAccount.getUserName() + "@@" + loginAccount.getPassword()):"");
		Result result =new Result();
		//StringUtils.isEmpty()判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0 
		if (StringUtils.isEmpty(loginAccount.getUserName()) || StringUtils.isEmpty(loginAccount.getPassword())) {
			result.setCode("500");
			result.setMessage("账号或密码不允许为空");
			return result;
		}

		if (loginAccount.getUserName().equals("1") && loginAccount.getPassword().equals("123")) {

			loginAccount.setUserId(UUID.randomUUID().toString());
			result.setDataMap(loginAccount);
			result.setCode("200");
			result.setMessage("登录成功");
		} else {
			result.setCode("500");
			result.setMessage("账号或密码无效");

		}

		return result;
	}


Then the following path according to the HTTP port this procedure, the root path and a path consisting of @RequestMapping localhost: 8099 / api / user / login

2. Get request method

Because no like @RequestMapping @RequestMapping (value = "addUser", method = {RequestMethod.POST}) assignment request or a POST method is GET, this is both.

3. To obtain the corresponding request parameters

Find the corresponding parameter in a format in POSTMAN. The present embodiment is a parameter loginAccount User object, object definitions to find User Field.

public @Data class User implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String userName;
	
	private String userId;
	
	private String text;

	private String password;

	private String phone;


}
Here there are five Filed the User class, the BODY POSTMAN the writing, according to the return click Send result to the API determines whether the test passed.

This embodiment has the user, corresponding to returns the correct result, the interface test is successful.


 

Published 99 original articles · won praise 43 · views 160 000 +

Guess you like

Origin blog.csdn.net/mayanyun2013/article/details/75645276