Java web开发--springboot

这篇具有很好参考价值的文章主要介绍了Java web开发--springboot。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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接口

Java web开发--springboot,后端

完全没有问题

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

本文由 mdnice 多平台发布文章来源地址https://www.toymoban.com/news/detail-538673.html

到了这里,关于Java web开发--springboot的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • JavaWeb开发(后端Web开发【一】)

    此文档来源于网络,如有侵权,请联系删除! 什么是Maven Maven是apache旗下的一个开源项目,是一款用于管理和构建java项目的工具。 Apache Apache 软件基金会,成立于1999年7月,是目前世界上最大的最受欢迎的开源软件基金会,也是一个专门为支持开源项目而生的非盈利性组织。

    2024年02月15日
    浏览(40)
  • 【Web后端架构】2023年10个最佳Web开发后端框架

    这些都是2023年在Java、Ruby、Python、JavaScript、PHP、Scala和Golang中进行web开发的最佳后端开发框架 在本文中,我将与您分享创建web应用程序的10个最好的后端框架。 Web开发通常分为两类:前端开发和后端开发。后端开发人员负责构建web应用程序的服务器端。 当前端部分与用户交

    2024年02月07日
    浏览(31)
  • Web后端开发

        通过各种插件实现项目的标准化构建。    1.5.1 当前工程环境 1.5.2 全局环境  1.8.1 依赖配置 1.8.2  依赖传递 pom.xml——右键——Diagrams——show dependences——显示依赖关系图   1.8.3 依赖范围  1.8.4 生命周期    2.3.1 概述  2.3.2 请求协议   2.3.3 响应协议   2.3.4 协议解析  

    2024年01月21日
    浏览(24)
  • 后端web开发之maven

    注意 maven属于项目一级,所以在创建项目的时候 直接选择maven项目按照步骤创建即可,而不是在空的project项目下创建maven模块,应该在maven项目下创建 作用1 作用2 作用3 在项目的pom.xml文件(也就是项目的源文件)里配置一个dependencies标签,里面输入坐标信息,这里的标记文本

    2024年02月09日
    浏览(28)
  • Web后端开发(请求响应)上

    浏览器(请求)--------------------------(HTTP协议)----------------------(响应)Web服务器 请求 :获取请求数据 响应 :设置响应数据 BS架构: 浏览器/服务器架构模式。客户端只需要浏览器,应用程序的逻辑和数据都存储在服务端(维护方便、体验一般) CS架构: 客户端/服务器架构

    2024年02月10日
    浏览(29)
  • java springboot VUE粮食经销系统开发mysql数据库web结构java编程计算机网页源码maven项目

    一、源码特点   springboot VUE 粮食经销系统是一套完善的完整信息管理类型系统,结合springboot框架和VUE完成本系统,对理解JSP java编程开发语言有帮助系统采用springboot框架(MVC模式开发) ,系统具有完整的源代码和数据库,系统主要采用B/S模式开发。 springboot vue 粮食经销系统

    2024年02月05日
    浏览(35)
  • JAVA-web后端

    controller 只是简单的java的一个类 java意义规范的技术 servlet tomcat也称为servlet容器 Interger age springboot会自动类型转换 post请求参数在请求体body from表单 x-www-form-urlencoded 中携带 将所有的请求参数都封装在实体类当中 默认是封装到数组,封装到集合要加注解@RequestParam 将多个请求

    2024年04月16日
    浏览(51)
  • [JavaWeb]【七】web后端开发-MYSQL

    目录 一 数据库介绍 1.1 什么是数据库  1.2 数据库产品 二 MySQL概述 2.1 下载 2.2 解压 2.3 配置 2.3.1. 添加环境变量 2.3.2. 初始化MySQL 2.3.3. 注册MySQL服务 2.3.4. 启动MySQL服务  2.3.5. 修改默认账户密码 2.4 登录MySQL 2.5卸载MySQL 2.2 数据模型 2.3 SQL简介 三 数据库设计-DDL 3.1 数据库操作 3

    2024年02月12日
    浏览(33)
  • [JavaWeb]【六】web后端开发-请求响应

    目录 一  引子 二 请求 2.1 Postman 2.1.1 安装 2.1.2 创建工作空间  2.1.3 添加接口 2.2 简单参数 2.2.1 原始方式(不推荐)  2.2.2 SpringBoot方式-GET(参数名与形参变量名相同)  2.2.3 SpringBoot方式-POST(参数名与形参变量名相同) 2.2.4 SpringBoot方式-POST(参数名与形参变量名不相同) 2.2.5 @Req

    2024年02月12日
    浏览(33)
  • [JavaWeb]【十四】web后端开发-MAVEN高级

    目录 一、分模块设计与开发 1.1 分模块设计  1.2 分模块设计-实践​编辑 1.2.1 复制老项目改为spring-boot-management 1.2.2 新建maven模块runa-pojo 1.2.2.1 将原项目pojo复制到runa-pojo模块  1.2.2.2 runa-pojo引入新依赖 1.2.2.3 删除原项目pojo包 1.2.2.4 在spring-boot-management(原项目)引入runa-pojo依赖

    2024年02月11日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包