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

相关推荐
952363 小时前
MyBatis
后端·spring·mybatis
uzong7 小时前
9 种 RAG 架构,每位 AI 开发者必学:完整实战指南
后端
小江的记录本7 小时前
【Kafka核心】架构模型:Producer、Broker、Consumer、Consumer Group、Topic、Partition、Replica
java·数据库·分布式·后端·搜索引擎·架构·kafka
止语Lab7 小时前
从手动到框架:Go DI 演进的三个拐点
开发语言·后端·golang
Daybreak10 小时前
Elasticsearch 里的索引和 Mapping,到底是什么关系?
后端
Lee川10 小时前
Prisma 实战指南:像搭积木一样设计古诗词数据库
前端·数据库·后端
李小狼lee10 小时前
深入浅出sse协议,用代码自己实现
后端
SamDeepThinking11 小时前
并发量就算只有2,该上锁还得上呀
java·后端·架构