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

alt

完全没有问题

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

本文由 mdnice 多平台发布

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值