其他分享
首页 > 其他分享> > 测试开发进阶——spring boot——MVC——@RequestMapping 是一个用来处理请求地址映射的注解,可用于类或方法上

测试开发进阶——spring boot——MVC——@RequestMapping 是一个用来处理请求地址映射的注解,可用于类或方法上

作者:互联网

@RequestMapping 是一个用来处理请求地址映射的注解,可用于类或方法上。

 

用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径;

 

用于方法上,表示在类的父路径下追加方法上注解中的地址将会访问到该方法,此处需注意@RequestMapping用在类上可以没用,但是用在方法上必须有。

 

=======================================================================

 

用在类和方法上:

 

package com.awaimai.web;

import org.hibernate.validator.constraints.*;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import java.util.Enumeration;

@Controller
@Validated
@RequestMapping(value = "/abc")
public class kzq
{



    @RequestMapping(value = "/test6")
    @ResponseBody
    public String test6()
    {

        return "123";

    }


}

 

  

 

 

web访问:

 

 

 

 

 

==============================================================================================

 

 

用在方法上:

 

package com.awaimai.web;

import org.hibernate.validator.constraints.*;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import java.util.Enumeration;

@Controller
@Validated
public class kzq
{



    @RequestMapping(value = "/test6")
    @ResponseBody
    public String test6()
    {

        return "123";

    }


}

 

  

 

 

 

web访问:

 

 

 

 

 

====================================================================

 

标签:进阶,spring,springframework,地址映射,org,import,validation,javax,constraints
来源: https://www.cnblogs.com/xiaobaibailongma/p/15111635.html