---
title: "springMVC @PathVariable中间带/问题处理"
author: "Dragonmandance"
date: "2016-08-05T14:32:56Z"
tags: []
categories: ["编程语言"]
slug: "springmvc-pathvariable"
draft: false
lastmod: "2016-08-05T14:32:56Z"
id: 2315768
url: "https://dragonmandance.iteye.com/blog/2315768"
---

```java

问题：请求地址/username/resourceUrl/methodName，其中username可能有也可能没有，resourceUrl中会带/，这个时候要使用@PathVariable，不能正确匹配controller

解决思路：把resourceUrl处理成一个不带/的参数即可
1、约定好/替换方案，比如请求方把/全部替换为--
2、通过url编码解码处理 / 经过编码变成%2F 把resourceUrl编码后，这个时候发现还是不能请求到正确的方法，因为到spring时已经自动解码了。可以把%2F再编一次码变成%252F。%编码后是25
/**
*/abc/xiaoming/h5/user.json/get
*/
@ResponseBody
    @RequestMapping(method=RequestMethod.POST ,value="/abc/{username}/{resourceUrl}/{methodName}")
    public String dubboMock(HttpServletResponse response,@PathVariable String username,@PathVariable String resourceUrl,@PathVariable String methodName){
		
    }
3、放弃使用PathVariable，手动去处理
 /**
*/abc/xiaoming/h5/user.json/get
*/
@ResponseBody
    @RequestMapping(method=RequestMethod.POST ,value="/abc/**")
    public String dubboMock(HttpServletResponse response,HttpServletResponse request){
	String url = request.getRequestURI();
        //处理url
    }

```