学习springboot过程中的问题之无法引入@requestmapping注解

本文介绍了在SpringBoot项目中遇到的@RequestMapping注解无法使用的问题及解决步骤。首先,问题源于缺少spring-web.jar依赖,通过在pom.xml中引入依赖并更新解决。其次,启动时访问后台报错是因为需要添加@ResponseBody注解以返回JSON或XML数据。再者,由于自动加载数据源导致启动报错,可排除DataSourceAutoConfiguration和HibernateJpaAutoConfiguration。最后,当控制器中多个方法返回相同结果时,是因为未正确设置@RequestMapping的value。总结了@RequestMapping的作用,并提供了相关学习资源链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.在创建好springboot maven项目之后,在controller中无法使用@requestmapping 注解。

因为缺少了 spring-web.jar包。需要在pom.xml中引入以下代码:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

引入之后,对该项目进行update 待包下载完毕,注解可以使用。如下

package com.test.simaple.contoller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/")
public class HelloController {

	
	@RequestMapping("/getHello")
	public String getHello(){
		
		return "";
	}
}


2.启动springboot  访问后台报错
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Apr 27 17:34:34 CST 2018
There was an unexpected error (type=Internal Server Error, status=500).
Circular view path [test success111111111111111111111]: would dispatch back to the current handler URL
 [/test success111111111111111111111] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view,
 due to default view name generation.)

后台代码:


	
	@RequestMapping("/getHello")
	
	public String getHello(){
		
		return "test success";
	}
	
	@RequestMapping()
	public String test(){
		
		return "test success111111111111111111111";
	}

后台返回json 和xml 数据类型需要在方法上添加@ResponseBody  注解。

@RequestMapping("/getHello")
	
	public String getHello(){
		
		return "test success";
	}
	
	@RequestMapping()
        @ResponseBody //新增这个注解
	public String test(){
		
		return "test success111111111111111111111";
	}


否则springboot会默认是要跳转页面,这个 时候无法找到 跳转的URL 就会报错


3.启动报错。如下:


错误原因: 因为在springboot 启动的时候,会自动加载datasource数据源,
解决方法:在类的上面添加@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
或者 在application.properties 配置文件中添加以下代码:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc\:mysql\://127.0.0.1\:3306/springboot
spring.datasource.username=root
spring.datasource.password=
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# \u521D\u59CB\u5316\u5927\u5C0F\uFF0C\u6700\u5C0F\uFF0C\u6700\u5927    
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# \u914D\u7F6E\u83B7\u53D6\u8FDE\u63A5\u7B49\u5F85\u8D85\u65F6\u7684\u65F6\u95F4
spring.datasource.maxWait=60000

4.项目能正常启动了,在controller 中写了两个方法,但访问只返回了一个结果

controller:

@Controller
public class HelloController {

	
	@RequestMapping("getHello")
	public String getHello(){
		
		return "test hello";
	}
	
	@RequestMapping()
	@ResponseBody
	public String test(){
		
		return "test";
	}
}

页面访问:localhost:8080/getHello        返回结果 ↓

页面访问:localhost:8080     返回结果 ↓


两个返回结果一样,但按代码里加上/getHello  应该返回  test hello   

。。。额。找了半天,没找到具体的明确的解释,不过还是可以总结一下,在@requestMapping ()  注解中添加value或者 “ /是用来指定http请求映射路径,和缩小 映射的范围。 如果不添加任何参数,就默认会把所有找不到路径的请求全部映射到此方法上,即使 你访问的是localhost:8080/ssdfsdf   ← 这样的, 还是返回   test。

 所以以后写代码还是按照规矩来,一个  /   都不能少


找到了两个介绍@requestMapping 的文章,想更详细的了解的话可以参考以下两个:

针对@requestMapping 注解value 的使用方法解释  ↓

 https://www.cnblogs.com/kuoAT/p/7121753.html 

全部注解 英文文档 加 中文解释 ↓

https://blog.youkuaiyun.com/qq_33186251/article/details/54599694




评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值