七牛云中带前缀的路径斜杆是怎么写的_Spring Controller RequestMapping 统一访问路径的前缀...

使用场景

例如,我想写一个 emoji 表情查询功能。相关的页面链接:

emoji 首页 /emoji

标签分类 /emoji/tag/tag-slug

具体标签详情 /emoji/emoji-slug

特征就是都以 emoji 作为网址路径前缀。

写个简单测试 Controller

返回字符串看看效果。

@RestController

@RequestMapping("/emoji")

public class EmojiController {

@GetMapping("")

public String index() {

return "hello index";

}

@GetMapping("/tag/{slug}")

public String tag(@PathVariable("slug") String slug) {

return "hello tag " + slug;

}

}

测试结果:

http://localhost:9090/emoji/tag/smile

hello tag smile

果然可以。

首页不想缀上斜杠

例如访问

http://localhost:9090/emoji

报错:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Dec 18 17:00:55 CST 2020

There was an unexpected error (type=Not Found, status=404).

No message available

而加上后缀斜杠就正常:

http://localhost:9090/emoji/

hello index

解决方法:

@GetMapping("/")

替换为:

@GetMapping("")

这样的好处是,同时支持了加斜杠后缀和不加的情况。

参考

https://www.oreilly.com/library/view/spring-cookbook/9781783985807/ch03s06.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值