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

相关推荐
追逐时光者6 小时前
一款开源、现代化的 WinForm UI 控件库
后端·.net
花月C7 小时前
个性化推荐:基于用户的协同过滤算法
开发语言·后端·算法·近邻算法
cci8 小时前
还在用conda?,试试uv,提高包的安装速度
后端
cci8 小时前
设备每次插入Linux识别的串口不一样?试试udev!
后端
9ilk9 小时前
【C++】--- C++11
开发语言·c++·笔记·后端
码事漫谈10 小时前
VSCode CMake Tools 功能解析、流程与最佳实践介绍
后端
火云牌神10 小时前
本地大模型编程实战(38)实现一个通用的大模型客户端
人工智能·后端
码事漫谈10 小时前
从C++/MFC到CEF与TypeScript的桌面架构演进
后端
冰块的旅行11 小时前
magic-api使用
后端
用户895356032822011 小时前
Goroutine + Channel 高效在哪?一文吃透 Go 并发底层 G-M-P 调度与实现
后端·go