Spring请求路径带参数URL使用注解的写法说明

目录
  • Spring请求路径带参数URL使用注解的写法
    • 正确写法:
    • 错误写法:
    • 小结:
  • Spring注解@RequestMapping请求路径映射问题

Spring请求路径带参数URL使用注解的写法

调用外部平台http接口,Post请求,url 为路径带有参数的形式:

http://xxxxxx.com/openApi/auth/getUserAuth?version=v1.0

使用 Retrofit 框架(本文使用的2.6.2版本)发送Post请求,可以在 @Post 注解中直接带上参数,如下:

    @POST("auth/getUserAuth?version=v1.0")
    Call<McgjResponse<UserAuthResponseDTO>> getUserAuth(@Body UserAuthRequest userAuthRequest);

因为初次使用 Retrofit 框架,所以自己启动Spring服务模拟外部平台接口,发现之前一直都在@PostMapping中定义路径,还没怎么写过带参数的,导致写错了,报 404错误,记录一下下。先说正确写法:

正确写法:

@PostMapping(value ="/authorize/addRecord",params = "version=v1.0")
    public McgjResponse<UserAuthResponseDTO> test(){

其实@RequestMapping、@GetMapping、@PostMapping 三个注解都可以指定请求Header、请求path、以及请求params。

@RequestMapping("/foo") 等价于 @RequestMapping(path="/foo")

 /**
  * The primary mapping expressed by this annotation.
  * <p>In a Servlet environment this is an alias for {@link #path}.
  * For example {@code @RequestMapping("/foo")} is equivalent to
  * {@code @RequestMapping(path="/foo")}.
  * <p>In a Portlet environment this is the mapped portlet modes
  * (i.e. "EDIT", "VIEW", "HELP" or any custom modes).
  * <p><b>Supported at the type level as well as at the method level!</b>
  * When used at the type level, all method-level mappings inherit
  * this primary mapping, narrowing it for a specific handler method.
  */
 @AliasFor("path")
 String[] value() default {};

所以平常在括号中直接写,只是指定了 path。如果错误地把参数写到请求 path 中,则会报 HTTP 404 错误,如下错误写法:

错误写法:

//错误写法
@PostMapping(value ="/auth/getUserAuth?version=v1.0")
    public McgjResponse<UserAuthResponseDTO> test(){

小结:

这三个注解平时用的是如此之多,却如此不熟悉,实在不应该!

Spring注解@RequestMapping请求路径映射问题

@RequestMapping请求路径映射,如果标注在某个controller的类级别上,则表明访问此类路径下的方法都要加上其配置的路径;最常用是标注在方法上,表明哪个具体的方法来接受处理某次请求。

以下两种方式都可以从url中传参数,但是第二种方式的适用性更高一些,当参数中包含中文的时候,如果用第一种方式传参数,经常会出现参数还没到controller就已经经过编码了(例如:经过utf-8编码后,原本要传的参数就会以%+ab...cd这样的方式出现),然后controller接受到这样的请求后,根本无法解析该请求应该走那个业务方法。

然后就会出现常见的404问题。。。

package com.test.jeofey.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; 

@Controller
@RequestMapping("/path")
public class TestController {

 // 第一种传参数的方式 访问地址例如:http:域名/path/method1/keyWord.html
 @RequestMapping("method1/{keyWord}")
 public String getZhiShiDetailData(@PathVariable("keyWord") String keyWord,
   HttpServletRequest request, HttpServletResponse response){
  System.out.println(keyWord);
  return "v1/detail";
 }

 // 第二种传参数的方式 访问地址例如:http:域名/path/method2.html?key=keyWord
 @RequestMapping("method2")
 public String getCommonData(HttpServletRequest request,
   HttpServletResponse response){
  String keyWord= request.getParameter("key");
  System.out.println(keyWord);
  return "v1/common";
 }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 基于SpringMVC中的路径参数和URL参数实例

    1.SpringMVC中的路径参数就是指在路径中添加参数,用于实现伪静态是很好的. 2.路径参数实现方式(一个Controller方法) @RequestMapping(value="/page/{name}/{age}",method=RequestMethod.GET) public String getName(ModelMap map,@PathVariable("name") String name,@PathVariable("age"

  • 浅谈SpringBoot处理url中的参数的注解

    1.介绍几种如何处理url中的参数的注解 @PathVaribale 获取url中的数据 @RequestParam 获取请求参数的值 @GetMapping 组合注解,是 @RequestMapping(method = RequestMethod.GET) 的缩写 (1)PathVaribale 获取url中的数据 看一个例子,如果我们需要获取Url=localhost:8080/hello/id中的id值,实现代码如下: @RestController public class Hello

  • springboot获取URL请求参数的多种方式

    1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交. /** * 1.直接把表单的参数写在Controller相应的方法的形参中 * @param username * @param password * @return */ @RequestMapping("/addUser1") public String addUser1(String username,String password) { System.out.pri

  • spring boot中controller的使用及url参数的获取方法

    类上加上@RequestMapping其访问的地址就是类上的加上方法上的菜能访问到该方法,例如上图的地址就是/hello/say @RequestMapping(value = "/hello",method = RequestMethod.GET) 和@GetMapping(value = "/hello")是等同的 这样就能获取url参数的值了,其结果如下 总结 以上所述是小编给大家介绍的spring boot中controller的使用及url参数的获取方法,

  • Spring根据URL参数进行路由的方法详解

    前言 本文主要介绍了关于Spring根据URL参数进行路由的相关内容,分享出来供大家参考学习价值,下面来一起看看详细的介绍吧. 发现问题 最近在写接口的时候发现一个问题,就是两个REST接口的URL的path部分是一样的,根据query传入不同的参数来区分. 比如S3普通上传接口是是: PUT /{bucketname}/{ objectname} 分块上传的接口是: PUT /{bucketname}/{objectname}?partNumber={partNumber}&uploadId=

  • Spring请求路径带参数URL使用注解的写法说明

    目录 Spring请求路径带参数URL使用注解的写法 正确写法: 错误写法: 小结: Spring注解@RequestMapping请求路径映射问题 Spring请求路径带参数URL使用注解的写法 调用外部平台http接口,Post请求,url 为路径带有参数的形式: http://xxxxxx.com/openApi/auth/getUserAuth?version=v1.0 使用 Retrofit 框架(本文使用的2.6.2版本)发送Post请求,可以在 @Post 注解中直接带上参数,如下

  • spring cloud gateway 如何修改请求路径Path

    一.背景 项目升级改造,老项目使用请求url中特定参数进行服务路由,现使用gateway网关进行路由服务信息 二.根据参数信息修改请求路径Path @Component public class RequestFilter implements GlobalFilter, Ordered { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpR

  • jquery+ajax请求且带返回值的代码

    现在比较流行使用jquery的ajax来实现一些无刷新请求效果,本章节提供一个非常简单的代码实例供大家参考之用,希望能够给需要的朋友带来一定的帮助,代码如下: <script type="text/javascript"> /* 请求Ajax 带返回值,并弹出提示框提醒 --------------------------------------------------*/ function getAjax(url,parm,callBack) { $.ajax({ typ

  • Spring Mvc中传递参数方法之url/requestMapping详解

    前言 相信大家在使用spring的项目中,前台传递参数到后台是经常遇到的事, 我们必须熟练掌握一些常用的参数传递方式和注解的使用,本文将给大家介绍关于Spring Mvc中传递参数方法之url/requestMapping的相关内容,分享出来供大家参考学习,话不多说,直接上正文. 方法如下 1. @requestMapping: 类级别和方法级别的注解, 指明前后台解析的路径. 有value属性(一个参数时默认)指定url路径解析,method属性指定提交方式(默认为get提交) @Reques

  • Spring源码之请求路径匹配路由方式

    目录 请求路径匹配路由 入口 进入上面方法 SpringMVC 将请求找到匹配的处理 初始化映射关系 从映射关系中寻找匹配方法 请求路径匹配路由 在spring中,当一个请求过来的时候会做路径匹配,下面我们就从源码层面分析一下路径匹配. 示例: @RequestMapping(value = "/user/{aid}/online/**", method = RequestMethod.GET) 我们一起看看这个方法是如何寻找的,和一些相应的工具类 入口 我的项目使用的是自动配置的Re

  • 为spring get请求添加自定义的参数处理操作(如下划线转驼峰)

    1.生成自己的注解(为了确定在哪些位置使用) /** * 关闭patch delete的model处理,否则会报错 */ @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface AliasProcessor { } /** * 处理Get 请求参数的驼峰问题 * @author lw */ @Target(ElementType.FIELD) @Retentio

  • Ajax轮询请求状态(微信公众号带参数二维码登录网站)

    这里要实现的功能是:通过扫码微信公众号带参数的二维码,来登录网站. 但很明显,如果ajax不间断的请求服务器,这样会加重服务器的负荷,所以本例采用的是js的setInterval来周期性调用执行一个ajax函数来来向服务器请求数据,但请求成功或者请求一定次数后还未成功时用clearinterval函数清空计时器. 代码和注释如下:(后端采用thinkPHP实现,所以js代码中含有一些thinkPHP的语法规则) <script type="text/javascript" src

  • Spring请求参数校验功能实例演示

    SpringMVC支持的数据校验是JSR303的标准,通过在bean的属性上打上@NotNull.@Max等进行验证.JSR303提供有很多annotation接口,而SpringMVC对于这些验证是使用hibernate的实现,所以我们需要添加hibernate的一个validator包: 依赖引用 compile 'javax.validation:validation-api:2.0.0.Final' compile 'org.hibernate:hibernate-validator:6

  • django views重定向到带参数的url

    当一个函数进行完成后需要重定向到一个带参数的url URL path('peopleapply/<int:jobid>/',second_views.peopleapply,name='peopleapply'), 函数 def peopleapply(request,jobid): 略 调用 return redirect('peopleapply',jobid = people.jbnum_id) peopleapply 为函数再url中的name jobid 为需要的参数名称 补充:DJ

  • C#发送Get、Post请求(带参数)

    目录 Get请求 Post请求 Get请求 1.简单发送Get请求 /// <summary> /// 指定Url地址使用Get 方式获取全部字符串 /// </summary> /// <param name="url">请求链接地址</param> /// <returns></returns> public static string Get(string url) { string result = &qu

随机推荐