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

相关推荐
山水洛行1 小时前
AI Agent 智能体记忆:从检索到被治理的数据系统
后端
卷无止境1 小时前
C++20 的概念与约束:让模板编程终于"说人话"
后端
Ai拆代码的曹操1 小时前
一次排查三种连接泄漏模式,再也不怕 HikariCP 连接池爆满了
后端
咪库咪库咪1 小时前
Cypher入门
后端
雪隐2 小时前
个人电脑玩AI-08让5060 Ti给你打工——我拿 Unlimited-OCR扫了 600 页书,然后悟了
人工智能·后端
AskHarries2 小时前
用 OpenClaw 做一份完整 PPT:从主题、提纲到 slide deck
后端·程序员
Csvn2 小时前
Linux 常用操作命令合集与运维实战
后端
卷无止境3 小时前
现代C++ 编译器生态及其对编程规范的影响
后端
云技纵横3 小时前
一个 @Async,把 @Transactional 的事务边界打穿了
后端·面试