《springboot实战》第一章 SpringBoot起步(上)

简介: 《springboot实战》第一章 SpringBoot起步(上)

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、初始化项目

https://start.spring.io/

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;
    }


目录
相关文章
|
4月前
|
前端开发 Java 数据库
基于springboot的书籍学习平台
基于springboot的书籍学习平台
|
XML Java 测试技术
SpringBoot入门篇 01、Springboot入门及配置(二)
SpringBoot入门篇 01、Springboot入门及配置(二)
|
15小时前
|
SQL Java 数据库连接
SpringBoot入门(十三)
SpringBoot入门(十三)
|
11月前
|
Java 编译器 数据格式
SpringBoot快速实践
启动一个SpringBoot项目 如果你觉得使用官网来创建太慢了,那你直接把以前项目的依赖粘过来就行了: 一个是父工程的依赖: <!--指定了一个父工程,父工程中的东西在该工程中可以继承过来使用--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.0</version> </parent> <!--JDK 的版本-->
30 0
|
XML 前端开发 JavaScript
《springboot实战》第一章 SpringBoot起步(下)
《springboot实战》第一章 SpringBoot起步(下)
107 0
|
SQL 存储 Java
SpringBoot基础学习文章
SpringBoot基础学习文章
187 0
SpringBoot基础学习文章
|
Java 应用服务中间件 Maven
《SpringBoot篇》01.Springboot超详细入门(基础篇)(一)
《SpringBoot篇》01.Springboot超详细入门(基础篇)(一)
224 0
《SpringBoot篇》01.Springboot超详细入门(基础篇)(一)
|
druid Java 关系型数据库
《SpringBoot篇》01.Springboot超详细入门(基础篇)(三)
《SpringBoot篇》01.Springboot超详细入门(基础篇)(三)
485 0
《SpringBoot篇》01.Springboot超详细入门(基础篇)(三)
|
JSON Java 应用服务中间件
《SpringBoot篇》01.Springboot超详细入门(基础篇)(二)
《SpringBoot篇》01.Springboot超详细入门(基础篇)(二)
171 0
《SpringBoot篇》01.Springboot超详细入门(基础篇)(二)
|
开发框架 负载均衡 前端开发
SpringBoot入门篇 01、Springboot入门及配置(一)
SpringBoot入门篇 01、Springboot入门及配置(一)