也会有许多的资源可以供大家学习分享金沙网址:

服务端程序本质上也只是个Java程序,它接受顾客端的输入,然后将总括管理后的再次回到值再次回到给客商端。下边大家就以那一个思路带头Java后端之旅吧。

作者从事在线教育多年,将自个儿的素材结合建了一个QQ群,对于风野趣一齐调换学习java的能够加群:732976516,里面有大神会给予解答,也有过多的财富能够供大家学习共享,款待我们前来联合念书升高!

引用Spring Boot库

拍卖HTTP央浼之类的政工,大家要求库的协助。所以率先步我们就把Spring
Boot引进进来。

不供给其余工具,大家选用maven来管理库依赖,那样大家假如写一个pom.xml就好了。大家先写三个最简的pom.xml。首如果概念groupId,比如是笔者司,还应该有artifactId,正是利用的实际名字:

xmlns:xsi=””

xsi:schemaLocation=”
;

4.0.0

cn.alios.system.service.prefix

Prefix

1.0.0-SNAPSHOT

加多父援用

左近于类的后续,大家不是伊始开采,而是继续Spring Boot
Starter框架。增多parent的剧情如下:

org.springframework.boot

spring-boot-starter-parent

2.1.2.RELEASE

2.1.2是本文写作时,Spring Boot的风靡版本。

充实正视

为了活动下载库,大家将急需的几个库增加到pom.xml中的重视项中。那样maven就能够帮大家从旅社中下载最新的库代码。

小编们供给AOP和Web多少个包,用全名是spring-boot-starter-aop和spring-boot-starter-web:

org.springframework.boot

spring-boot-starter-aop

org.springframework.boot

spring-boot-starter-web

援引插件

Spring Boot还提供了插件,大家也将其引述进来:

org.springframework.boot

spring-boot-maven-plugin

主函数

库引用完了,大家就写叁个主程序吧。依照常规,我们将其保存在src/main/java目录下:

package cn.alios.system.service.prefix;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

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

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

@SpringBootApplication

@RequestMapping

public class Prefix {

@RequestMapping

@ResponseBody

public String home(){

return “Hello, Java Web World!”;

}

public static void main(String[]金沙网址, args) throws Exception{

SpringApplication.run(Prefix.class,args);

}

}

编译

上边我们用mvn package命令来编译生成可运维的jar包:

mvn package

出口临近于下边那样:

[INFO] Scanning for projects…

[INFO]

[INFO] —————< cn.alios.system.service.prefix:Prefix
>—————-

[INFO] Building Prefix 1.0.0-SNAPSHOT

[INFO] ——————————–[ jar
]———————————

[INFO]

[INFO] — maven-resources-plugin:3.1.0:resources (default-resources)
@ Prefix —

[INFO] Using ‘UTF-8’ encoding to copy filtered resources.

[INFO] Copying 0 resource

[INFO] Copying 0 resource

[INFO]

[INFO] — maven-compiler-plugin:3.8.0:compile (default-compile) @
Prefix —

[INFO] Changes detected – recompiling the module!

[INFO] Compiling 1 source file to
/Users/ziyingliuziying/working/gitlab/Prefix/target/classes

[INFO]

[INFO] — maven-resources-plugin:3.1.0:testResources
(default-testResources) @ Prefix —

[INFO] Using ‘UTF-8’ encoding to copy filtered resources.

[INFO] skip non existing resourceDirectory
/Users/ziyingliuziying/working/gitlab/Prefix/src/test/resources

[INFO]

[INFO] — maven-compiler-plugin:3.8.0:testCompile
(default-testCompile) @ Prefix —

[INFO] Nothing to compile – all classes are up to date

[INFO]

[INFO] — maven-surefire-plugin:2.22.1:test (default-test) @ Prefix

[INFO] No tests to run.

[INFO]

[INFO] — maven-jar-plugin:3.1.1:jar (default-jar) @ Prefix —

[INFO] Building jar:
/Users/ziyingliuziying/working/gitlab/Prefix/target/Prefix-1.0.0-SNAPSHOT.jar

[INFO]

[INFO] — spring-boot-maven-plugin:2.1.2.RELEASE:repackage
(repackage) @ Prefix —

[INFO] Replacing main artifact with repackaged archive

[INFO]

[INFO] BUILD SUCCESS

[INFO]

[INFO] Total time: 2.462 s

[INFO] Finished at: 2019-01-31T16:53:48+08:00

[INFO]

末段生成的包是target/Prefix-1.0.0-SNAPSHOT.jar。

运行

调用java -jar
target/Prefix-1.0.0-SNAPSHOT.jar命令,运转那个Java程序,输出如下:

. ____ _ __ _ _

/\\ / ___’_ __ _ __ __ __ _ \ \ \ \

\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \

\\/ ___)| |_)| | | | | || ) )

‘ |____| .__|_| |_|_| |_\__, | / / / /

=========|_|==============|___/=/_/_/_/

:: Spring Boot :: (v2.1.2.RELEASE)

2019-01-31 16:59:43.144 INFO 95879 — [ main]
cn.alios.system.service.prefix.Prefix : Starting Prefix v1.0.0-SNAPSHOT
on ziyingliuziyingdeMacBook-Pro.local with PID 95879
(/Users/ziyingliuziying/working/gitlab/Prefix/target/Prefix-1.0.0-SNAPSHOT.jar
started by ziyingliuziying in
/Users/ziyingliuziying/working/gitlab/Prefix)

2019-01-31 16:59:43.148 INFO 95879 — [ main]
cn.alios.system.service.prefix.Prefix : No active profile set, falling
back to default profiles: default

2019-01-31 16:59:44.289 INFO 95879 — [ main]
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port:
8080

2019-01-31 16:59:44.325 INFO 95879 — [ main]
o.apache.catalina.core.StandardService : Starting service [Tomcat]

2019-01-31 16:59:44.325 INFO 95879 — [ main]
org.apache.catalina.core.StandardEngine : Starting Servlet engine:
[Apache Tomcat/9.0.14]

2019-01-31 16:59:44.347 INFO 95879 — [ 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/ziyingliuziying/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]

2019-01-31 16:59:44.435 INFO 95879 — [ main]
o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded
WebApplicationContext

2019-01-31 16:59:44.435 INFO 95879 — [ main]
o.s.web.context.ContextLoader : Root WebApplicationContext:
initialization completed in 1234 ms

2019-01-31 16:59:44.665 INFO 95879 — [ main]
o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService
‘applicationTaskExecutor’

2019-01-31 16:59:44.886 INFO 95879 — [ main]
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port: 8080
with context path ”

2019-01-31 16:59:44.889 INFO 95879 — [ main]
cn.alios.system.service.prefix.Prefix : Started Prefix in 2.161 seconds
(JVM running for 2.561)

作者们能够见见,运行了三个9.0.14版本的Apache
汤姆cat服务器,在8080端口上监听。

我们开辟浏览器,访问, Java Web
World!』这几个字符串被呈现出来。

再写二个Controller

在主函数里面能够管理央求,那么再别的类里面该怎么做吧?大家透过写@Controller声明,加上@RequestMapping来钦赐路线,就足以了。

笔者们来写个例子:

package cn.alios.system.service.prefix.controller;

import org.springframework.stereotype.Controller;

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

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

@Controller

@RequestMapping

public class TestController {

@RequestMapping

@ResponseBody

public String test(){

return “Test Controller!”;

}

}

还是mvn package,然后java -jar java -jar
target/Prefix-1.0.0-SNAPSHOT.jar。

在浏览器里试下 Controller!』

功勋卓著告成!现在一切从收到输入到体现输出的坦途早就打通,是或不是很easy?

相关文章