WEB

SpringBoot学习知识点以及出错记录

2018-11-12  本文已影响10人  我问你瓜保熟吗

一、 application.properties配置

# 连接mysql
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver


二、@RequestMapping 和 @GetMapping @PostMapping 区别

@RequestMapping 和 @GetMapping @PostMapping 区别
@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。
@PostMapping是一个组合注解,是@RequestMapping(method = RequestMethod.POST)的缩写。

三、HttpServerletRequest

HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象提供的方法,可以获得客户端请求的所有信息。

常用方法:

getParameter(String name) 获取name的参数值
getParameterValues(String name) 获取name的所有参数值,返回一个数组
getRequestURL 方法返回客户端发出请求时的完整URL。
getRequestURI 方法返回请求行中的资源名部分。
getQueryString 方法返回请求行中的参数部分。
getRemoteAddr 方法返回发出请求的客户机的IP地址。

打war包

1.先将默认的打包方式由jar更换为war,打开pom修改<packaging>jar</packaging>为

<packaging>war</packaging>

2.在pom.xml文件中添加如下的依赖:

这一步是将springboot自带Tomcat替换为我们外部自己的Tomcat启动,注意: provided是将内部Tomcat屏蔽了,这样本地使用的话是不能启动项目的,所以最好的办法是将<scope>provided</scope>删掉,不影响打包发布也同时保证了本地正常的使用,我这里注释掉了

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
   <!--<scope>provided</scope>-->
</dependency> 

3.在启动类DemoApplication中更改代码

添加继承SpringBootServletInitializer,并重写configure方法:

package com.springboot.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
        System.out.println("外部tomcat,chapter启动!");
        return application.sources(DemoApplication.class);
    }
}

4、打开idea右边栏的maven工具,执行package,等待打包完成,显示build success

5、将target目录里的war包丢到tomcat里,访问时地址后加上包名访问即可

四、Myhabits出错记录

mysql8.0连接出错: authentication plugin 'caching_sha2_password'

MySQL8.0.4开始默认使用新的认证插件"caching_sha2_password",更改默认使用mysql_native_password:
default_authentication_plugin=mysql_native_password

解决:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';

错误

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NumberFormatException: null] with root cause

解决:修改版本到5.1.46

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.46</version>
</dependency>
上一篇下一篇

猜你喜欢

热点阅读