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

相关推荐
copyer_xyf8 小时前
Python 异常处理
前端·后端·python
llz_1129 小时前
web-第三次课后作业
前端·后端·web
MageGojo11 小时前
天气 API 接入实战:基于 ApiZero 实现实时天气、分钟级降水和 15 天预报查询
java·后端·spring·api 接口接入·接口实战
pe7er12 小时前
软件设计不要“既要又要”
前端·后端·架构
柏舟飞流12 小时前
Spring Boot 深入实践指南:从入门到工程化落地
spring boot·后端·firefox
IT_陈寒12 小时前
Java Stream并行流的坑:我花了3小时才找到的线程安全问题
前端·人工智能·后端
橘子海全栈攻城狮12 小时前
【最新源码】鸟博士微信小程序 023
spring boot·后端·web安全·微信小程序·小程序
Hiter_John13 小时前
Golang的运算符
开发语言·后端·golang
皮皮林55113 小时前
Dubbo 的 SPI 和 JDK 的 SPI 有什么区别?
后端
金銀銅鐵13 小时前
用 Tkinter 实现一个罗马数字转整数的简单工具
后端·python