映射不同的路径到相同的方法

controller层

package com.zhl.practice.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Holley
 * @Description 请输入一句话进行描述
 * @create 2018-09-28 10:09
 **/
@RestController
@RequestMapping("/test")
public class TestController {

    @RequestMapping(value = {"/name1","/name2"},produces = {"application/json;charset=UTF-8"})
    public String test(HttpServletRequest request){

        return "url:" + request.getRequestURL().toString();
    }

}

路径访问/test/name1或者/test/name2时调用的是同一个方法

produces是用来设置返回的response的媒体类型和字符集的,如上表示返回值是json对象

原文地址:https://www.cnblogs.com/zhlblogs/p/9717247.html