1、下载和安装maven工具
https://maven.apache.org/download.cgi
下载解压,修改conf下的setting.xml,优先从阿里云镜像拉取关联包
<mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. | <mirror> <id>mirrorId</id> <mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> --> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> <mirror> <id>maven.net.cn</id> <name>oneof the central mirrors in china</name> <url>http://maven.net.cn/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> <mirror> <id>central</id> <name>Maven Repository Switchboard</name> <url>http://repo1.maven.org/maven2/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors>
修改本地maven库路径
<localRepository>d:\localRepository</localRepository>
2、第一个spring boot 项目
2.1、初始化项目
2.2、配置maven
2.3、启动项目
在pom.xml添加web依赖,集成内嵌tomcat
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2.4、定义一个hello world接口
2.4.1、在Controller包下创建类,并编写hello world接口
2.4.2、通过浏览器访问接口
地址:http://localhost:8080/hello
2.5、解决接口跨域问题
2.5.1、类或方法解决接口跨域问题
1、在class或method上加上【@CrossOrigin(“*”)】,解决跨域问题
2.5.2、配置全局跨域
新建【config】package,添加自定义的跨域java文件
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class MyWebMvcConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedHeaders("*") .allowedMethods("*") .maxAge(1800) .allowedOrigins("*"); } }
3、接收参数
3.1、通过HttpServletRequest 接收参数
前端访问路径:http://127.0.0.1/user/login?userName=zs&pwd=123
后端Java代码:
@RequestMapping("/login") public Map login(HttpServletRequest request , HttpServletResponse response) { // URL: http://127.0.0.1/user/login?userName=zs&pwd=123 Map map = new HashMap(); map.put("code" , 200); map.put("msg" , "success"); //第一种方式接收入参 String userName = request.getParameter("userName"); String pwd = request.getParameter("pwd"); return map; }
3.2、通过@RequestParam接收参数
前端访问路径:http://127.0.0.1/user/login?userName=zs&pwd=123
后端Java代码:
@RequestMapping("/login") public Map login(@RequestParam(value="name" ,required=true) String userName, @RequestParam(value="pwd" ,required=true) String password, HttpServletResponse response) { // @RequestParam(value="name" ,required=true) required=true传入的值不允许为空 // URL: http://127.0.0.1/user/login?userName=zs&pwd=123 //第二种方式接收入参 System.out.println(userName + "||" + password); Map map = new HashMap(); map.put("code" , 200); map.put("msg" , "success"); return map; }