当前位置: 首页 > wzjs >正文

东营做网站营销推广方案案例

东营做网站,营销推广方案案例,网站服务设计,网站怎么做下载网页代码本文参考转载:https://oldmoon.top/post/191 简介 使用最新版的Springboot 3.2.1(我使用3.2.0)搭建开发环境进行开发,调用接口时出现奇怪的错。报错主要信息如下: Name for argument of type [java.lang.String] not specified, and paramet…

本文参考转载:https://oldmoon.top/post/191

简介

使用最新版的Springboot 3.2.1(我使用3.2.0)搭建开发环境进行开发,调用接口时出现奇怪的错。报错主要信息如下:

Name for argument of type [java.lang.String] not specified, and parameter name information not available via reflection. Ensure that the compiler uses the ‘-parameters’ flag.

原因分析

首先,这是Spring新版本导致的。为什么会出现这个问题呢?原来是Spring 6.1之后,官方加强了很多错误校验和报错提示,本文这个错也是其中之一。

Spring表示:URL中的传参,必须使用@PathVariable声明用于接收的变量,如:

@DeleteMapping("/employees/{employeeId}") 
public String deleteEmployee(@PathVariable int employeeId) { ... 
} @PatchMapping("/employees/{id}/{firstName}") 
public String patchEmployee(@PathVariable Integer id, @PathVariable String firstName) { ... 
}

官方说明中一直强调@PathVariable的使用,并没有提及@RequestParam,参考官方文档@RequestParam会发现最后有一句话:

Note that use of@RequestParamis optional (for example, to set its attributes). By default, any argument that is a simple value type (as determined by BeanUtils#isSimpleProperty) and is not resolved by any other argument resolver, is treated as if it were annotated with@RequestParam.

翻译一下大概是:

注意@RequestParam的使用是可选的(例如,设置其属性)。 默认情况下,任何简单值类型(由 BeanUtils#isSimpleProperty 确定)且未由任何其他参数解析器解析的参数都将被视为使用@RequestParam注解。

根据原文及翻译,这自然让我认为,@RequestParam依然是可以省略的。

然而奇怪的是,当Springboot 3.2.1使用Maven管理项目时,如果不使用spring-boot-starter-parent作为父工程,那么接口中必须显式声明@RequestParam("name"),缺了其中的name也会报错。我清晰地记得我在旧版本的 Springboot 中经常省略 @RequestParam(“name”) 这种写法。

但如果不使用spring-boot-starter-parent作为父工程,好像@RequestParam变成了不可省略注解。大家搭建微服务和多模块时候,通常不会使用spring-boot-starter-parent作为父工程吧?还是只有我不用?。。。 还是尽量不要尝试新版本,会少踩很多坑

  • 错误代码

当请求URL中有正常参数时,如:http://localhost:8080/user/hello?name=zhangsan,其中name为一个参数,你的Controller代码大概如下所示:

java

@GetMapping("/hello") 
public RespPack<?> hello(String name) { return null; 
}
  • 主要pom.xml
  •   <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
    

解决

这种现象不知道是不是官方的BUG,但目前我发现几种解决方案:

  1. 在参数上使用@RequestParam("name")

  2. 使用spring-boot-starter-parent

    <!-- 将spring-boot-starter-parent作为父工程在pom.xml中引入 --> 
    <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.1</version> <relativePath/> 
    </parent>
    
  3. maven-compiler-plugin

    网友提除解决方案:父pom或本身pom中添加maven-compiler-plugin的配置:

    <build> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.12.0</version><configuration> <parameters>true</parameters> </configuration> </plugin>
    </build> 
    

这可确保使用-parameters标志编译代码,从而使参数名称在运行时可用。

http://www.dtcms.com/wzjs/237362.html

相关文章:

  • wordpress更新5.2信阳seo优化
  • windowxp做网站服务器b站推广渠道
  • 河南郑州网站关键词排名助手世界球队实力排名
  • 一台服务做两个网站吗专业做网站的公司
  • 网站界面设计的基本原则是什么百度网盘网页版官网
  • 网站建设预算百度地图网页版进入
  • c#做的网站怎么上传seo优化系统
  • 成都科技网站建设找北京seo多少钱
  • 公司 网站源码站长seo综合查询
  • 网站开发 教程百度招聘网最新招聘信息
  • 用php写的网站有哪些糕点烘焙专业培训学校
  • 网站含中国的备案东莞网站营销推广
  • wordpress 加cdn宁波seo推广推荐公司
  • 温州网站建设制作推广app的软文案例
  • 学网站开发培训企业文化墙
  • 网站做多大的宽高国际新闻头条
  • 南京做网站哪家公司好b2b国际贸易平台
  • 后端工资一般比前端高吗湖北搜索引擎优化
  • 做网站都需要年服务费吗百度首页优化
  • 网站上怎样做超链接网店关键词怎么优化
  • 深圳 网站建设培训学校百度员工收入工资表
  • 网站建设前端学什么语言百度指数在哪里看
  • php 公安网站源码广告推销网站
  • 天门市住房和城乡建设委员会网站小红书推广方案
  • 湘潭做网站口碑好磐石网络推广营销方案
  • 产品企业网站的优化建议
  • 北京市人大网站建设白杨seo课程
  • 网站开发要学哪些新型网络营销模式
  • 怎么做网站后期推广朋友圈营销
  • 怎么建立网站免费的seo资源网站排名