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 小时前
从输入 juejin.cn 到看到页面:DNS、Nginx、服务器集群与 CDN 到底在做什么?
后端·mysql
东风破_6 小时前
从 0 设计一个博客数据库:用户、头像与文章表应该怎么建?
后端·mysql
大鸡腿同学7 小时前
黄仁勋点透的 AI 真相:能实现你知道但做不到的,却实现不了你不知道的
后端
FfHUCisI7 小时前
Go 编译过程全景
开发语言·后端·golang
FfHUCisI7 小时前
Golang 语法分析与 AST:Parser 与 go/ast
开发语言·后端·golang
++==9 小时前
RESTful详解:核心思想、框架、与HTTP的区别,API的设计风格
后端·http·restful
IT_陈寒10 小时前
Vue的响应式更新有时候真的不听话
前端·人工智能·后端
向生11 小时前
Ubuntu Caddy 保姆级完整教程
后端
tntxia11 小时前
Docker权限的问题
后端
明月_清风11 小时前
看完这段关于"全插件化架构"的技术分析后,我整理了一份笔记
前端·后端