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

相关推荐
stark张宇2 分钟前
分布式事务方案设计与选型思考:消息队列
后端
想不到一个好的ID1 小时前
什么是线程安全
后端
Dreams_l1 小时前
基于SpringBoot实现的抽奖系统(四)
java·spring boot·后端
程序员cxuan1 小时前
阿里又来一王炸,QBot 能组团干活了
人工智能·后端·程序员
用户2339861795731 小时前
TypeScript源码解析:权限系统与 OS 级沙箱
后端
程序员cxuan2 小时前
WorkBuddy 接入 Step 5,干活太爽了
人工智能·后端·程序员
BigTopOne3 小时前
02 use:引入路径
后端
BigTopOne3 小时前
09 match 与 if let:模式匹配
后端
BigTopOne3 小时前
4. 容易误解的地方:`pub` 不一定真的公开
后端
BigTopOne3 小时前
06 trait:定义行为约定
后端