zoukankan      html  css  js  c++  java
  • 1. Hello World

    Create a new Maven quickstart app project in Eclipse

    1. 参考此篇文章创建Maven项目(b). Web,在Eclipse创建一个基于Maven的quick start项目。其中archetype和Arfifact Id设置如下。

    2. 修改pom.xml文件,增加spring依赖。

      pom.xml
       1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
       3   <modelVersion>4.0.0</modelVersion>
       4   <groupId>com.xuan.flight.tutorial</groupId>
       5   <artifactId>hello-world-springws</artifactId>
       6   <version>1.0.0</version>
       7   <packaging>jar</packaging>
       8   <name>hello-world-springws</name>
       9   <url>http://maven.apache.org</url>
      10   <parent>
      11         <groupId>org.springframework.boot</groupId>
      12         <artifactId>spring-boot-starter-parent</artifactId>
      13         <version>1.4.1.RELEASE</version>
      14     </parent>
      15     <dependencies>
      16         <dependency>
      17             <groupId>org.springframework.boot</groupId>
      18             <artifactId>spring-boot-starter-web</artifactId>
      19         </dependency>
      20         <dependency>
      21             <groupId>org.springframework.boot</groupId>
      22             <artifactId>spring-boot-starter-test</artifactId>
      23             <scope>test</scope>
      24         </dependency>
      25         <dependency>
      26             <groupId>com.jayway.jsonpath</groupId>
      27             <artifactId>json-path</artifactId>
      28             <scope>test</scope>
      29         </dependency>
      30     </dependencies>
      31     <properties>
      32         <java.version>1.7</java.version>
      33         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      34     </properties>
      35  
      36     <build>
      37         <plugins>
      38             <plugin>
      39                 <groupId>org.springframework.boot</groupId>
      40                 <artifactId>spring-boot-maven-plugin</artifactId>
      41             </plugin>
      42         </plugins>
      43     </build>
      44     <repositories>
      45         <repository>
      46             <id>spring-releases</id>
      47             <url>https://repo.spring.io/libs-release</url>
      48         </repository>
      49     </repositories>
      50     <pluginRepositories>
      51         <pluginRepository>
      52             <id>spring-releases</id>
      53             <url>https://repo.spring.io/libs-release</url>
      54         </pluginRepository>
      55     </pluginRepositories>
      56 </project>



    Implement Spring Web Service

    1. 在包com.xuan.flight.tutorial.springws下,增加Greeting实体类。

      Greeting.java
       1 package com.xuan.flight.tutorial.springws;
       2  
       3 public class Greeting
       4 {
       5     private final long id;
       6     private final String content;
       7     public Greeting(long id, String content)
       8     {
       9         this.id = id;
      10         this.content = content;
      11     }
      12     public long getId()
      13     {
      14         return id;
      15     }
      16     public String getContent()
      17     {
      18         return content;
      19     }
      20 }
       
    2. 在包com.xuan.flight.tutorial.springws下,增加GreetingController.java文件。实现请求处理器,在Spring中由@RestController 标记实现。

       
       1 package com.xuan.flight.tutorial.springws;
       2  
       3 import java.util.concurrent.atomic.AtomicLong;
       4 import org.springframework.web.bind.annotation.RequestMapping;
       5 import org.springframework.web.bind.annotation.RequestParam;
       6 import org.springframework.web.bind.annotation.RestController;
       7  
       8 @RestController
       9 public class GreetingController
      10 {
      11     private static final String template = "Hello, %s!";
      12     private final AtomicLong counter = new AtomicLong();
      13  
      14     @RequestMapping("/greeting")
      15     public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name)
      16     {
      17         return new Greeting(counter.incrementAndGet(), String.format(template, name));
      18     }
      19 }
    3. 在包com.xuan.flight.tutorial.springws下,修改已经存在的App.java文件,实现服务入口逻辑。借助于Spring的支持,我们可以将Tomcat容器嵌入到当前的程序中,让服务能够以独立的Java程序启动,无需额外部署到外部Tomcat容器中。

    4.  1 package com.xuan.flight.tutorial.springws;
       2  
       3 import org.springframework.boot.SpringApplication;
       4 import org.springframework.boot.autoconfigure.SpringBootApplication;
       5  
       6 @SpringBootApplication
       7 public class App
       8 {
       9     public static void main(String[] args)
      10     {
      11         SpringApplication.run(App.class, args);
      12     }
      13 }

    Compile & Run

    1. Eclipse默认已经自动编译完成,直接按照普通的Java程序运行即可。右键App.java -> Run As -> Java Application开始运行。
      在控制台可以看到如下的日志输出:Tomcat在8080端口作为嵌入式容器启动;当看到Started App in xxx seconds时,表明服务启动成功。
    2. 在浏览器输入http://localhost:8080/greeting或者http://localhost:8080/greeting?name=User可以看到类似下面的输出,表明服务运行正常。

      {"id":1,"content":"Hello, World!"}

    Read More

    1. Building a RESTful Web Service - https://spring.io/guides/gs/rest-service/
  • 相关阅读:
    [转载]详解网络传输中的三张表,MAC地址表、ARP缓存表以及路由表
    网络诊断小结
    Java代理模式示例程序
    [转载]Java中继承、装饰者模式和代理模式的区别
    [转载]JDK、SDK、J2EE、J2SE、J2ME的区别
    Java Web-JSTL
    [转载]Linux 命令详解:./configure、make、make install 命令
    jmeter之beanshell断言实例
    Appium左右、上下滑动(Java)
    【Maven】如何使用pom.xml引入自定义jar包
  • 原文地址:https://www.cnblogs.com/xxuan/p/6739792.html
Copyright © 2011-2022 走看看