SpringMVC Restful风格

在controller的类上添加@RestController注解

java 复制代码
package com.xiaoya.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller
@RestController
public class TestController {
    @RequestMapping("/hello/{name}")
    @ResponseBody
    public String hello(@PathVariable String name){
        return "hello "+name;
    }
}

在方法的参数前面标上@PathVariable注解,然后路径中使用{name}就可以解析变量

启动tomcat,访问http://localhost:8080/springmvc1_war_exploded/hello/2

相关推荐
mldong3 小时前
从 mldong 到 jeeflow:一个工作流引擎的独立进化
后端
陈随易3 小时前
MoonBit抓包模块pcap,查看电脑的每一次联网通信
前端·后端·程序员
Aaron - Wistron3 小时前
Web API C# (Furion版)带 单元测试
开发语言·后端·c#
卷无止境4 小时前
写代码这件事,到底该讲究点什么?
后端·python
卷无止境4 小时前
循环复杂度到底在算什么,Python 代码怎么才能写得让人一看就懂
后端·python
IT_陈寒5 小时前
Vite热更新失效?你可能漏了这个配置
前端·人工智能·后端
SomeB1oody6 小时前
【RustyML入门】1.0. 快速上手
开发语言·后端·机器学习·rust·教程
史呆芬7 小时前
分布式事务实战:微服务跨服务数据一致性解决方案
java·后端·spring cloud