springboot系列一 第一个restful接口 main方法启动 jar包启动 mvn启动(2.1.0.RELEASE版本)...

springboot官方文档

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#_working_with_spring_boot

maven依赖

spring-boot-starter-parent

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.example</groupId>
	<artifactId>myproject</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.0.RELEASE</version>
	</parent>

	<!-- Additional lines to be added here... -->

</project>

web依赖

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

启动类和接口

@RestController
@EnableAutoConfiguration
public class HelloWorldApp {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

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

注解解释

@RestController

@RestController = @Controller + @ResponseBody

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller //springmvc的注解
@ResponseBody //springmvc的注解
public @interface RestController {
    @AliasFor(
        annotation = Controller.class
    )
    String value() default "";
}

官方文档解释

The @RestController and @RequestMapping annotations are Spring MVC annotations. (They are not specific to Spring Boot.)
解释: @RestController 和 @RequestMapping 是springmvc的注解,没什么特殊性

@EnableAutoConfiguration

This annotation tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly.
@EnableAutoConfiguration 猜测地给开发者配置好有关springmvc、tomcat、spring方面的配置。也就是会自动配置好springmvc、tomcat、spring的一些配置

启动服务和访问接口

启动main方法,控制台打印:

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

2018-11-21 15:03:33.622  INFO 75693 --- [           main] com.yimingkeji.helloworld.HelloWorldApp  : Starting HelloWorldApp on yangzhenlongdeMacBook-Pro.local with PID 75693 (/Users/yangzhenlong/projs/my/yimingkeji/springboot/helloword/target/classes started by yangzhenlong in /Users/yangzhenlong/projs/my/yimingkeji/springboot)
2018-11-21 15:03:33.626  INFO 75693 --- [           main] com.yimingkeji.helloworld.HelloWorldApp  : No active profile set, falling back to default profiles: default
2018-11-21 15:03:34.499  INFO 75693 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-11-21 15:03:34.516  INFO 75693 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-11-21 15:03:34.516  INFO 75693 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.12
2018-11-21 15:03:34.522  INFO 75693 --- [           main] 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: [/Users/yangzhenlong/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2018-11-21 15:03:34.581  INFO 75693 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-11-21 15:03:34.582  INFO 75693 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 917 ms
2018-11-21 15:03:34.604  INFO 75693 --- [           main] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-11-21 15:03:34.607  INFO 75693 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-11-21 15:03:34.608  INFO 75693 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-11-21 15:03:34.608  INFO 75693 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'formContentFilter' to: [/*]
2018-11-21 15:03:34.608  INFO 75693 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-11-21 15:03:34.763  INFO 75693 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2018-11-21 15:03:34.933  INFO 75693 --- [           main]
o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-11-21 15:03:34.936  INFO 75693 --- [           main] com.yimingkeji.helloworld.HelloWorldApp  : Started HelloWorldApp in 1.727 seconds (JVM running for 2.849)

日志中看到 项目启动端口(prot)为8080,启动根路径(path)为' '

浏览器或者http客户端(如postman)访问 localhost:8080

Hello World!

使用jar包启动

在pom.xml依赖中添加spring-boot-maven-plugin插件

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

在终端shell或者Terminal中执行命令:

$ mvn package

在项目target目录下会生成jar包:

启动jar包

$ java -jar target/helloword-1.0.0-SNAPSHOT.jar 
2018-11-21 15:12:35.077  INFO 75841 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2018-11-21 15:12:35.355  INFO 75841 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-11-21 15:12:35.359  INFO 75841 --- [           main] com.yimingkeji.helloworld.HelloWorldApp  : Started HelloWorldApp in 2.382 seconds (JVM running for 2.838)

访问 localhost:8080

Hello World!

按 Ctrl + C 结束服务。

使用mvn启动

$ mvn spring-boot:run

项目地址

https://gitee.com/yimingkeji/springboot

转载于:https://my.oschina.net/yimingkeji/blog/2885473

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
restful restful所需要的jar包 ========================================= Restlet, a RESTful Web framework for Java ========================================= http://www.restlet.org ----------------------------------------- Native REST support * Core REST concepts have equivalent Java classes (UniformInterface, Resource, Representation, Connector for example). * Suitable for both client-side and server-side web applications. The innovation is that that it uses the same API, reducing the learning curve and the software footprint. * Restlet-GWT module available, letting you leverage the Restlet API from within any Web browser, without plugins. * Concept of "URIs as UI" supported based on the URI Templates standard. This results in a very flexible yet simple routing with automatic extraction of URI variables into request attributes. * Tunneling service lets browsers issue any HTTP method (PUT, DELETE, MOVE, etc.) through a simple HTTP POST. This service is transparent for Restlet applications. Complete Web Server * Static file serving similar to Apache HTTP Server, with metadata association based on file extensions. * Transparent content negotiation based on client preferences. * Conditional requests automatically supported for resources. * Remote edition of files based on PUT and DELETE methods (aka mini-WebDAV mode). * Decoder service transparently decodes compressed or encoded input representations. This service is transparent for Restlet applications. * Log service writes all accesses to your applications in a standard Web log file. The log format follows the W3C Extended Log File Format and is fully customizable. * Powerful URI based redirection support similar to Apache Rewrite module. Available Connectors * Multiple server HTTP connectors available, based on either Mortbay's Jetty or the Simple framework or Grizzly NIO framework. * AJP server connector available to let you plug behind an Apache HTT
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值