import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/***
* helloWord 工程,常用功能测试
*/
@RestController
@RequestMapping("/HelloWord")
public class HelloWordController {
protected final Logger logger = LoggerFactory.getLogger(getClass());
/**
* POST http://localhost:8080/HelloWord/helloWord
* Content-Type: application/x-www-form-urlencoded
*
* string=string
* @param request
* @return
*/
@RequestMapping("helloWord")
public String helloWord(HttpServletRequest request){
Map<String, String[]> parameterMap = request.getParameterMap();
logger.info("获取参数参数:{}", JSON.toJSONString(parameterMap));
String string = request.getParameter("string");
if (StringUtils.isEmpty(string)){
string = "helloWord!";
}
System.out.println(string);
return string;
}
/***
* 从请求地址变量获取参数
*/
@RequestMapping("login/{gameId}")
public int login(@PathVariable("gameId") int gameId) {
return gameId;
}
/***
* 获取请求接口名
* http://localhost:8080/HelloWord/getUrl
* 打印
* HelloWord
* getUrl
*/
@RequestMapping("getUrl")
public void getUrl(HttpServletRequest request) {
String uri = request.getRequestURI();
//截取掉第一个"/",不然数组中第一个值是""
String uriStr = uri.substring(1, uri.length());
//根据"/"拆分成数组
String[] arrayUri = uriStr.split("/");
for(int i = 0; i < arrayUri.length; i++){
System.out.println(arrayUri[i]);
}
}
}
11-29
3814
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
03-18
2218
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-01
2万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)