栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2021.11.08端口占用问题解决、Springboot的初级使用、多种注解的解释,网络错误代码,配置文件的替换

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021.11.08端口占用问题解决、Springboot的初级使用、多种注解的解释,网络错误代码,配置文件的替换

1、端口占用问题解决:

修改Tomcat的端口号,将占用端口的软件删除进程

查看当前端口被占用的命令:
win+R输入cmd
netstat - ano | findstr 8080

可以看到第一个占用了端口号8080
找到占用的进程命令: tasklist |findstr 13408
之后将进程退出,就可以使用该端口了。

如果在命令行中没有占用改端口的进程,那么就启动任务管理器。将java.exe全部结束

2、Spring Boot2的功能介绍:
在Spring Boot中,正常是不存在xml配置的。但是只是不推荐,并不是不支持

Spring Boot 主要提供了如下功能:

  1. 为所有基于 Spring 的 Java 开发提供方便快捷的入门体验。
  2. 开箱即用,有自己自定义的配置就是用自己的,没有就使用官方提供的默认的。
  3. 提供了一系列通用的非功能性的功能,例如:嵌入式服务器、安全管理、健康检测等。
  4. 绝对没有代码生成,也不需要XML配置。

3、Spring boot 创建

1、IDEA 新建New Projict > Spring Initializr > 使用官网的方式创建。之后创建Maven项目。打包方式为war包,语言为java。然后勾选工具,一般我们用到的:

web:spring Web、

SQL:Mybatis framework,

Mysql:Driver 、Developer Tools :Lombok
之后就会自动导入。

2、也可以通过阿里云的官网进行配置再下载。

3、Spring Boot的pom.xml配置



    4.0.0

    com.ne
    neu_shopping
    1.5.2

    war

    
        1.8
        UTF-8
        UTF-8
        2.5.6
        2.2.0
        3.8.1
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            ${mybatis-spring.version}
        

        
            mysql
            mysql-connector-java
            8.0.27
            runtime
        
        
            org.projectlombok
            lombok
            1.18.22
            true
        

        
        
            javax.servlet
            jstl
        

        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
            provided
        

        
            org.springframework.boot
            spring-boot-starter-test
            ${spring-boot.version}
            test
        
        
            junit
            junit
        
    

    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                ${maven-compiler.version}
                
                    1.8
                    1.8
                    UTF-8
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                ${spring-boot.version}
                
                    com.neu.springbootdemo01.SpringbootDemo01Application
                
                
                    
                        repackage
                        
                            repackage
                        
                    
                
            
        
    

就会进行自动导包了。如果pom报红,说明包没完全导入,修改一些jdk和包的路径就行了。

此时的代码列表应该是:

在HelloController中

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello Spring Boot";
    }

}

这时候就可以通过spring,访问web项目了
,地址栏输入:http://localhost:8080/hello,就可以看到index.jsp的内容。

4、解决依赖下载不完整的问题


在idea的配置中找到Maven, 本地打开Maven home directiry的目录,将这个文件夹删除,再对pom.xml进行配置。耐心等待几分钟,就可以下载完成。原因。下载不成功有可能是之前的没有下载完整,重新下载可能进行冲突。所以重新下载就可以了/

重新下载完成,再更新一下,就可以使用了

我们在pom.xml中加入

    war



        
            javax.servlet
            jstl
        



        
            org.apache.tomcat.embed
            tomcat-embed-jasper
        

新的pom文件:



    4.0.0
    com.neu
    springboot-demo01
    0.0.1-SNAPSHOT
    springboot-demo01
    Demo project for Spring Boot-在阿里云镜像官网直接创建SpringBoot项目
    war
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.6
         
    

    
        1.8
        UTF-8
        UTF-8
        2.5.6
        2.2.0
        3.8.1
    



    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            ${mybatis-spring.version}
        

        
            mysql
            mysql-connector-java
            8.0.27
            runtime
        
        
            org.projectlombok
            lombok
            1.18.22
            true
        



        
            javax.servlet
            jstl
        



        
            org.apache.tomcat.embed
            tomcat-embed-jasper
        


        
            org.springframework.boot
            spring-boot-starter-test
            ${spring-boot.version}
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                ${maven-compiler.version}
                
                    1.8
                    1.8
                    UTF-8
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                ${spring-boot.version}
                
                    com.neu.springbootdemo01.SpringbootDemo01Application
                
                
                    
                        repackage
                        
                            repackage
                        
                    
                
            
        
    



同时在src下新建webapp目录,(我们在修改pom文件之后就可以看到,出现了一个小十字符号,说明打包方式是war,可以应用。),在该目录下新建index.jsp
此时重新运行spring,直接打开还是404,但是在地址栏输入http://localhost:8080/index.jsp就可以看到我们写的内容。那么我们如何才能打开8080端口就直接访问index.jsp呢?

5、Spring中application.properties配置文件讲解
这个文件名不能随便进行修改,只能是指定的。

内容:

# 应用名称
spring.application.name=springboot-demo01

# 应用服务 WEB 访问端口,可以进行修改
server.port=8080


#下面这些内容是为了让MyBatis映射
#指定Mybatis的Mapper文件
mybatis.mapper-locations=classpath:mappers/*xml
#指定Mybatis的实体目录
mybatis.type-aliases-package=com.neu.springbootdemo01.mybatis.entity

# 数据库驱动:,这里的cj是5.0版本以后必须有的,如果是5.0版本以前的就可以不写
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据源名称
spring.datasource.name=defaultDataSource
# 数据库连接地址,这里的连接地址,需要和数据库的连接地址一致,否则就无法正确的连接
spring.datasource.url=jdbc:mysql://localhost:3306/shopping01?characterEncoding=UTF-8&serverTimezone=UTC

# 数据库用户名&密码:
spring.datasource.username=root
spring.datasource.password=123456


数据库的驱动,为什么会有cj呢?

因为在导入的库中,cj是一个路径。

6、Spring中的视图解析器

在Spring中其实没有给我们进行默认的视图配置,如果不信我们可以进行视图解析器的尝试

@Controller
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
}

在通常的项目中,这样进行注解的配置,一般如果在webapp中有hello.jsp就会可以打开hello.jsp,但是在spring中,你不管将hello.jsp放在webapp/hello.jsp还是放置webapp/WEB-INF/jsp/hello.jsp中。都无法打开。而且报错都是:

报错翻译是:查看你的视图解析器有没有进行配置。

我们该如何解决这个视图解析器的问题呢?
我们可以在application.properties文件最下层中加入:

#视图解析器:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

我们在地址栏输入:http://localhost:8080/hello,就可以正常的访问了。
问题:为什么我们的注解不同时,视图表现得不同?
当注解为@RestController时,我们访问http://localhost:8080/hello页面,出现得是hello。但是当注解为@Controller时,还需要配置application.properties才可以出现正确得页面呢?

7、@RestController和@Controller的区别,多种注解的解释
新建一个entity》User.java

package com.neu.springbootdemo01.entity;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class User {
    private String username;
    private String password;
}

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    @GetMapping("user")
    public User u1(){
        return new User("小娃","12346");

    }
}

这里idea虽然是报错的,但是仍然可以运行。而且可以将数值返回出来

可以看出,这里的响应格式就是json格式的。所以表明@RestController是返回json格式的。
在User中,我们还用了一个lombok的注解:

@AllArgsConstructor,所有参数的构造器

@NoArgsConstructor,无参构造器

 //@AllArgsConstructor
    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }
    //@NoArgsConstructor
    public User() {
    }

@Date相当于产生get和set方法 toString方法。

注释@GetMapping("/user")和之前的@RequestMapping(value="/user",method= RequestMethod.GET)意义相同。都是使用GET方法。

注释@ResponseBody的意思是代表返回类型为json
注释@RequestBody的意思是请求类型为json

原因: 注解为@RestController:表示返回数据类型为JSON
注解为@Controller:如果是字符串类型String,表示返回默认视图名称。

8、网络错误代码:

400(Bad Request):错误的请求,请求格式不一致,参数类型不一致,请求出现语法错误

404(Not Found):未找到,没有找到目录或者文件

405(Method Not Allwed):用来访问本页面的HTTP谓词不被允许,方法不允许。请求方法(GET、POST、HEAD、delete、Put、Trace等)对指定资源不适用

100:Cintunue请求已接受,客户应当继续发送请求的其余部分。

200:OK 一切正常,对GET和POST的请求的应答文档在后面。

500:内部服务器错误

501:未实现

502:网关错误

9、appliction.yml文件
用途和appliction.properties一致,但是优先级要比appliction.properties低。
为什么两个文件用途一致,还需要有两个呢?原因:
在appliction.properties文件中,我们在一些配置之前都需要写spring.xxxx.xxxxx再写配置参数。增加了我们的代码量。yum文件就可以简化我们的代码量。
内容:

server:
  #应用端口号
  port: 9999



spring:
  #应用名
  application:
    name: springboot-demo01
    #数据源
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    name: defaultDataSource
    password: root
    url: jdbc:mysql://localhost:3306/shopping01?characterEncoding=UTF-8&serverTimezone=UTC
    username: 123456

#视图解析器:
  mvc:
    view:
      prefix: /WEB-INF/jsp/
      suffix: .jsp

  #Mybatis
mybatis:
  mapper-locations: classpath:mappers/*xml
  type-aliases-package: com.neu.springbootdemo01.mybatis.entity

可以和上面的appliction.properties进行对比。这个yml文件使用缩进的路径同一级的进行缩进,可以简化很多appliction.properties中重复的代码,而且这个yml文件,配置的名称和配置的参数颜色不同,很容易进行区分。

附上appliction.properties转换为appliction.yml格式的网址:

https://toyaml.com/index.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/458443.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号