Java web开发--springboot

news2024/9/19 16:37:22

Java web开发--springboot

Java有很多web框架

但是有的框架不是很好用:比如Java Servlets(个人感觉)不好调试,WEB-INF文件关联来关联去很烦躁,启动后 crtl+c还关闭不了(我一般习惯用ctrl+c命令来关闭服务).导致后面我调试springboot时一直报错,原来是Java Servlets的服务没关(我习惯性ctrl+c没关掉,也没提示),端口占用的问题

介绍spring boot的使用

我介绍我使用的vscode的wsl Linux子环境比较简单的方法

像去网站上填参数然后下载一个压缩包解压对新手来说太不友好,我参数都看不懂什么意思

新建项目springboot

我直接使用maven命令构建一个新spring boot项目

mvn archetype:generate -DgroupId=com.example -DartifactId=demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

其中的-DgroupId=com.example -DartifactId=demo根据自己的需要填写

配置pom.xml

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.1.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>9</java.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>

  
</project>

编写简单的代码

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.classargs);
    }
  }

有些import不需要,包警告的就删掉.

直接调试代码,查看环境是否有问题,一般是不会有什么问题的

返回一般是这样环境就没问题了;如果有报错再自己修复吧

 .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '
_ | '_| | '_ \/ _` | \ \ \ \
\\/  ___)| |_)| | | | | || (_| |  ) ) ) )
'  |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::        (v2.0.1.RELEASE)

2023-07-07 17:42:29.412  INFO 32491 --- [           main] com.example.App                          : Starting App on LAPTOP-MOE7TCNL with PID 32491 (/root/java/springboottest/target/classes started by root in /root/java/springboottest)
2023-07-07 17:42:29.414  INFO 32491 --- [           main] com.example.App                          : No active profile set, falling back to default profiles: default
2023-07-07 17:42:29.464  INFO 32491 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4562e04d: startup date [Fri Jul 07 17:42:29 CST 2023]; root of context hierarchy
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (file:/root/.m2/repository/org/springframework/spring-core/5.0.5.RELEASE/spring-core-5.0.5.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2023-07-07 17:42:30.373  INFO 32491 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-07-07 17:42:30.406  INFO 32491 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-07-07 17:42:30.407  INFO 32491 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.29
2023-07-07 17:42:30.418  INFO 32491 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib]
2023-07-07 17:42:30.506  INFO 32491 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-07-07 17:42:30.506  INFO 32491 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1048 ms
2023-07-07 17:42:30.620  INFO 32491 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2023-07-07 17:42:30.625  INFO 32491 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: '
characterEncodingFilter' to: [/*]
2023-07-07 17:42:30.625  INFO 32491 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: '
hiddenHttpMethodFilter' to: [/*]
2023-07-07 17:42:30.625  INFO 32491 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: '
httpPutFormContentFilter' to: [/*]
2023-07-07 17:42:30.626  INFO 32491 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: '
requestContextFilter' to: [/*]
2023-07-07 17:42:30.732  INFO 32491 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2023-07-07 17:42:30.958  INFO 32491 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4562e04d: startup date [Fri Jul 07 17:42:29 CST 2023]; root of context hierarchy
2023-07-07 17:42:31.038  INFO 32491 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/index],methods=[GET]}" onto public com.example.App$MyResponse com.example.App.getIndex()
2023-07-07 17:42:31.045  INFO 32491 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2023-07-07 17:42:31.046  INFO 32491 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2023-07-07 17:42:31.075  INFO 32491 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2023-07-07 17:42:31.075  INFO 32491 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2023-07-07 17:42:31.233  INFO 32491 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2023-07-07 17:42:31.281  INFO 32491 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path '
'
2023-07-07 17:42:31.290  INFO 32491 --- [           main] com.example.App                          : Started App in 2.201 seconds (JVM running for 2.499)
2023-07-07 17:42:40.207  INFO 32491 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet '
dispatcherServlet'
2023-07-07 17:42:40.208  INFO 32491 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet '
dispatcherServlet': initialization started
2023-07-07 17:42:40.222  INFO 32491 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet '
dispatcherServlet': initialization completed in 14 ms
2023-07-08 00:01:14.340  INFO 32491 --- [       Thread-2] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4562e04d: startup date [Fri Jul 07 17:42:29 CST 2023]; root of context hierarchy
2023-07-08 00:01:14.484  INFO 32491 --- [       Thread-2] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

最后再写一个get请求json数据响应看看基本功能

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.classargs);
    }

    @GetMapping("/index")
    public MyResponse getIndex() {
        return new MyResponse(100"ok""hello world!");
    }


    private static class MyResponse {
        private int code;
        private String msg;
        private String data;

        public MyResponse(int code, String msg, String data) {
            this.code = code;
            this.msg = msg;
            this.data = data;
        }

        // Getters and setters

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }
    }
}

没使用的方法可以删掉

执行结果:

访问http://localhost:8080/index接口

alt

完全没有问题

这样就可以探究其他的东西了,例如请求数据的接受处理,数据库连接,rises等等深度探索

本文由 mdnice 多平台发布

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/729833.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Handshake failed due to invalid Upgrade header: null 解决方案以及连接60s,信息不交互,连接断开

Handshake failed due to invalid Upgrade header: null 解决方案以及连接60s&#xff0c;信息不交互&#xff0c;连接断开 1. 问题背景&#xff1a;因为后端用了nginx代理&#xff0c;所以websocket连接的过程中报错&#xff1a;Handshake failed due to invalid Upgrade hea…

[MySQL]数据类型(图文详解)

[MySQL]数据类型 文章目录 [MySQL]数据类型1. 数据类型分类2. 数值类型2.1 bit类型2.2 tinyint类型2.3 float类型2.4 decimal类型2.5 char类型2.6 varchar类型2.7 日期和时间类型2.8 enum和set类型 1. 数据类型分类 2. 数值类型 说明&#xff1a; 数值类型在定义时不加unsign…

【雕爷学编程】Arduino动手做(154)---AFMotor电机扩展板模块

37款传感器与执行器的提法&#xff0c;在网络上广泛流传&#xff0c;其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块&#xff0c;依照实践出真知&#xff08;一定要动手做&#xff09;的理念&#xff0c;以学习和交流为目的&am…

备忘录模式的学习与使用

1、备忘录模式的学习 当您需要在不破坏封装性的前提下保存和恢复对象的状态时&#xff0c;备忘录模式是一种有用的设计模式。备忘录模式允许您将对象的状态保存到备忘录对象中&#xff0c;并在需要时从备忘录中恢复状态。这种模式分离了状态的保存和恢复逻辑&#xff0c;使得对…

stm32usart+中断接收定长数据

文章目录 前言一、cubemx的配置二、代码编写1.主函数在这里插入图片描述2. it.c 总结 前言 中断接收固定的数据值。 一、cubemx的配置 开启USART1 打开NVIC中断 二、代码编写 1.主函数 #define RxBuf_SIZE_1 50 //USART1接收缓冲区大小 #define MainBuf_SIZE_1 50 //USAR…

实现会议管理功能

目录 生成原始会议数据 一、数据结构 二、添加测试数据 查询会议列表分页数据&#xff08;后端&#xff09; 一、业务分析 二、编写持久层代码 三、编辑业务层代码 四、编写Web层代码 查询成员列表&#xff08;后端&#xff09; 一、编写持久层代码 二、编写业务层…

Mybatis-plus——查询条件设置+字段映射和表名映射

查询条件设置 等匹配查询 假如要进行登录查询的话&#xff0c;需要用到如下的等匹配&#xff0c;在实际的登录中应该做md5加密&#xff0c;从查询条件取出密码。 //条件查询LambdaQueryWrapper<mpdb> lqwnew LambdaQueryWrapper<mpdb>();//模拟登陆查询等匹配lqw…

SELF-ATTENTION DOES NOT NEED O(n2) MEMORY

背景 主要是要解决self-attention空间复杂度的问题&#xff0c;因为对于gpu计算来说&#xff0c;内存空间非常宝贵&#xff0c;序列长度较长的时候会出现oom问题。 用线性时间解决self-attention问题 解决数据稳定问题 因为由于进行求和计算&#xff0c;容易导致浮点数超过最…

栈区和堆区以及注意事项

>>栈区 #include<stdio.h> #include<string.h> #include<stdlib.h>int* func() {int a 10;//栈上创建的变量return &a; }void test01() {int* p func();//结果早已不重要&#xff0c;因为上面的a早已被释放&#xff0c;再去操作这块内存属于//非…

行业追踪,2023-07-04,受特斯拉中报影响,汽车零部件放量强势拉升,不调整

自动复盘 2023-07-04 成交额超过 100 亿 排名靠前&#xff0c;macd柱由绿转红 成交量要大于均线 有必要给每个行业加一个上级的归类&#xff0c;这样更能体现主流方向 rps 有时候比较滞后&#xff0c;但不少是欲杨先抑&#xff0c; 应该持续跟踪&#xff0c;等 macd 反转时参与…

Gitbash常用指令总结

Part I git 开始的指令 1、git init&#xff1a;初始化一个Git仓库&#xff1b; 2、git clone&#xff1a;从远程仓库克隆代码到本地&#xff1b; 直接使用网址 git clone <url>or 用a代替网址 git remote add a <url>git clone a3、git add&#xff1a;添加文件到…

计算机基础--->数据结构(8)【B树、B+树<超详细图文>】

文章目录 B树(B-Tree)B树的查询操作B树的几种插入删除情况 B树B树的主要特点插入操作删除操作 B树(B-Tree) B树&#xff08;B-Tree&#xff09;是一种自平衡的搜索树&#xff0c;又称平衡多路查找树&#xff0c;主要用于系统中大量数据的读和写操作。B树的特点是能保持数据有序…

English-英语语法体系

语法体系 英语中的所有语句类型&#xff1a;什么怎么样 英语语法最重要的就是动词&#xff01;

3.4.流的学习,异步任务的管理

目录 前言1. 流总结 前言 杜老师推出的 tensorRT从零起步高性能部署 课程&#xff0c;之前有看过一遍&#xff0c;但是没有做笔记&#xff0c;很多东西也忘了。这次重新撸一遍&#xff0c;顺便记记笔记。 本次课程学习精简 CUDA 教程-流的学习&#xff0c;异步任务的管理 课程大…

MySQL库表操作作业

创建数据库 mysql> create database Market; mysql> use Market; 创建表和约束 mysql> create table customers(c_num int(11) primary key not null UNIQUE Key auto_increment , -> c_name varchar(50), -> c_city varchar(50), -> c_birth datetime…

九、HTML中的定位

1、定位 position static 默认值 没有使用定位 relactive 相对定位 absolute 绝对定位 fixed 锚定 标准文档流 标准文档流 从上到下&#xff0c;从左向右&#xff0c;依次显示网页中的每一个元素 元素分类 行内元素 依次一个挨着一个显示 块级元素 独占一行 static 定位 以…

【STM32智能车】智能寻迹

【STM32智能车】智能寻迹 基础算法寻迹小车 我们之前说了到了寻迹这里会涉及到一些算法&#xff0c;不过各位小伙伴可以放心&#xff0c;我们这里用的是一些基础算法。不需要公式&#xff0c;只需要进行简单的判断就行。 基础算法 寻迹车的程序算法如下&#xff1a; 初始化&…

MySQL数据库小练习1

1.创建数据库&#xff0c;删除数据库&#xff0c;查询创建数据的语句&#xff0c;使用数据库&#xff0c;查询当前默认的数据库以及使用的编码方式校验规则 创建数据库及使用数据库&#xff1a; create database hzc default character set utf8mb4 collate utf8mb4_0900_ai_…

uniapp电子签名以及竖屏签名后内容旋转90度变为横屏图片

用该插件挺不错的 电子签名插件地址 如果你一个页面要用多个该插件&#xff0c;就改成不同的cavas-id&#xff0c;修改插件源码 效果图 竖屏写 旋转成横屏图片 插件内 在拿到签名临时地址后的页面 <!-- 旋转图片canvas --> <canvas canvas-id"camCacnvs&quo…

第二次CCF计算机软件能力认证

第一题&#xff1a;相邻数对 给定 n 个不同的整数&#xff0c;问这些数中有多少对整数&#xff0c;它们的值正好相差 1。 输出格式 输入的第一行包含一个整数 n&#xff0c;表示给定整数的个数。 第二行包含所给定的 n 个整数。 输出格式 输出一个整数&#xff0c;表示值正好相…