欢迎大家关注我的公众号【老周聊架构】,Java后端主流技术栈的原理、源码分析、架构以及各种互联网高并发、高性能、高可用的解决方案。
根据自己的业务情况总结了一下有以下几种情况:
一、spring boot的启动类不能直接放在main(src.java.main)这个包下面,把它放在有包的里面就可以了。(springboot的启动的Application必须放在controller类的外面,要不然扫描不到) 如果你的启动类Application位置没错的话直接进入第二步。
二、检查Controller类里面的注解
package com.riemann.springbootdemo.controller;
import com.riemann.springbootdemo.model.ApiResponse;
import com.riemann.springbootdemo.model.ExportExcelData;
import com.riemann.springbootdemo.service.ExportExcelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping(value = "/export")
public class ExportExcelController {
@Autowired
private ExportExcelService exportExcelService;
@RequestMapping(value = "/exportExcel", method= RequestMethod.POST)
public ApiResponse exportExcel(@RequestBody ExportExcelData eeData) {
ApiResponse apiResponse = new ApiResponse();
if (eeData == null) {
return null;
} else {
apiResponse = exportExcelService.exportExcel(eeData);
}
return apiResponse;
}
}
该类注解注意点:
1、@Controller
注解,在对应的方法上,视图解析器可以解析return 的jsp,html页面,并且跳转到相应页面。若返回json等内容到页面,则需要加@ResponseBody
注解。我这边直接使用了@RestController
注解。所以@ResponseBody
注解可以直接不写。
2、@RestController
注解,相当于@Controller+@ResponseBody
两个注解的结合,返回json数据不需要在方法前面加@ResponseBody
注解了,但使用@RestController
这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面。
3、@Autowired
注解,注入Service
服务对象,不写该注解的话会报异常
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
4、@RequestMapping(value = "/exportExcel", method= RequestMethod.POST)
,我这个是Post请求,所以后面加上method为post请求;还有一种方式,直接写成 PostMapping(value = "/exportExcel")
5、@RequestBody
注解,我的是post请求,这是利用一个对象去获取前端传过来的数据;如果你是get请求,可以用 @PathVaribale
获取url中的数据 和 @RequestParam
获取请求参数的值
get请求示例:
(1)、PathVaribale 获取url路径的数据
请求URL:
localhost:8080/hello/id 获取id值
@RestController
public class HelloController {
@RequestMapping(value="/hello/{id}/{name}",method= RequestMethod.GET)
public String sayHello(@PathVariable("id") Integer id,@PathVariable("name") String name){
return "id:" + id + " name:" + name;
}
}
在浏览器中 输入地址:
localhost:8080/hello/27/riemann
输出:
id:27 name:riemann
(2)、RequestParam 获取请求参数的值
获取url参数值,默认方式,需要方法参数名称和url参数保持一致
localhost:8080/riemann?id=27
@RestController
public class HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String sayHello(@RequestParam Integer id){
return "id:" + id;
}
}
输出:
id:27
同理,有多个参数的话加后面加就行了。
(@RequestParam Integer id, @RequestParam String name)
三、检查Application类的注解
package com.riemann.springbootdemo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@MapperScan("com.riemann.springbootdemo.dao")
@ComponentScan(value = "com.riemann")
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
1、@SpringBootApplication
是一个复合注解,包括@ComponentScan
,和@SpringBootConfiguration
,@EnableAutoConfiguration
。
@SpringBootConfiguration
继承自@Configuration
,二者功能也一致,标注当前类是配置类,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到srping容器中,并且实例名就是方法名。@EnableAutoConfiguration
的作用启动自动的配置,@EnableAutoConfiguration
注解的意思就是Springboot根据你添加的jar包来配置你项目的默认配置,比如根据spring-boot-starter-web
,来判断你的项目是否需要添加了webmvc
和tomcat
,就会自动的帮你配置web项目中所需要的默认配置。在下面博客会具体分析这个注解,快速入门的demo实际没有用到该注解。@ComponentScan
,扫描当前包及其子包下被@Component
,@Controller
,@Service
,@Repository
注解标记的类并纳入到spring
容器中进行管理。是以前的<context:component-scan>
(以前使用在xml
中使用的标签,用来扫描包配置的平行支持)。所以本demo中的User为何会被spring
容器管理。
2、@MapperScan("com.riemann.springbootdemo.dao")
注解,扫描dao
层的接口的,没有这个注解的话,不能和数据库交互。
3、@ComponentScan(value = "com.riemann")
,这个注解坑了我一下当时,这个注解主要就是定义扫描的路径,从中找出标识了需要装配的类自动装配到spring
的bean
容器中。这个只需要写com.riemann
层,不需要写com.riemann.springbootdemo
。
以上就是我对springboot项目能正常访问,但接口调用的时候404错误的一些理解,如果有什么理解不到位的地方,欢迎指出。