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

相关推荐
一tiao咸鱼22 分钟前
前端转 agent # 03 - Pydantic v2 数据校验深入
前端·后端
一路向北North27 分钟前
Spring Security OAuth2.0(18):资源服务测试
java·后端·spring
用户99045017780091 小时前
若依工作流主流方案对接
后端
触底反弹1 小时前
🚀 Node.js path 和 fs 模块,从回调地狱到 async/await 的进化之路!
javascript·后端·node.js
Csvn2 小时前
Python 开发技巧 · 生成器(Generator)进阶 —— 从 yield 到 yield from,写出省内存的生产者-消费者模式
后端·python
geovindu2 小时前
CSharp: Dijkstra Algorithms
开发语言·后端·算法·c#
掘金者阿豪3 小时前
听书别再被会员和广告打断:把NAS变成自己的私人有声图书馆
后端
拉长的苗条3 小时前
细说 ASP.NET Cache 及其高级用法
后端·asp.net
一路向北North3 小时前
Spring Security OAuth2.0(19):JWT令牌
java·后端·spring
用户6757049885024 小时前
Beanstalkd 实战指南:原来延迟队列、异步任务可以如此简单丝滑!
后端·消息队列