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

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

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


        org.springframework.boot
        spring-boot-starter-data-jpa

引入之后,对该项目进行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 "";
	}
}

学习springboot过程中的问题之无法引入@requestmapping注解_第1张图片


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过程中的问题之无法引入@requestmapping注解_第2张图片

错误原因: 因为在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        返回结果 ↓

学习springboot过程中的问题之无法引入@requestmapping注解_第3张图片

页面访问: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.csdn.net/qq_33186251/article/details/54599694




你可能感兴趣的:(错误心得)