博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1. Hello World - WebService based on Spring
阅读量:4341 次
发布时间:2019-06-07

本文共 4921 字,大约阅读时间需要 16 分钟。

Create a new Maven quickstart app project in Eclipse

  1. 参考此篇文章创建Maven项目,在Eclipse创建一个基于Maven的quick start项目。其中archetype和Arfifact Id设置如下。
  2. 修改pom.xml文件,增加spring依赖。

    pom.xml
    1 
    3
    4.0.0
    4
    com.xuan.flight.tutorial
    5
    hello-world-springws
    6
    1.0.0
    7
    jar
    8
    hello-world-springws
    9
    http://maven.apache.org
    10
    11
    org.springframework.boot
    12
    spring-boot-starter-parent
    13
    1.4.1.RELEASE
    14
    15
    16
    17
    org.springframework.boot
    18
    spring-boot-starter-web
    19
    20
    21
    org.springframework.boot
    22
    spring-boot-starter-test
    23
    test
    24
    25
    26
    com.jayway.jsonpath
    27
    json-path
    28
    test
    29
    30
    31
    32
    1.7
    33
    UTF-8
    34
    35 36
    37
    38
    39
    org.springframework.boot
    40
    spring-boot-maven-plugin
    41
    42
    43
    44
    45
    46
    spring-releases
    47
    https://repo.spring.io/libs-release
    48
    49
    50
    51
    52
    spring-releases
    53
    https://repo.spring.io/libs-release
    54
    55
    56

     

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中由 标记实现。

     

     

    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 GreetingController10 {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. 在浏览器输入或者可以看到类似下面的输出,表明服务运行正常。

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

Read More

  1. Building a RESTful Web Service - 

转载于:https://www.cnblogs.com/xxuan/p/6739792.html

你可能感兴趣的文章
笛卡尔遗传规划Cartesian Genetic Programming (CGP)简单理解(1)
查看>>
初识前端作业1
查看>>
ffmpeg格式转换命令
查看>>
万方数据知识平台 TFHpple +Xpath解析
查看>>
Hive实现oracle的Minus函数
查看>>
秒杀多线程第四篇 一个经典的多线程同步问题
查看>>
RocketMQ配置
查看>>
蚂蚁金服井贤栋:用技术联手金融机构,形成服务小微的生态合力
查看>>
端口号大全
查看>>
机器学习基石笔记2——在何时可以使用机器学习(2)
查看>>
POJ 3740 Easy Finding (DLX模板)
查看>>
MySQL 处理重复数据
查看>>
关于typedef的用法总结(转)
查看>>
【strtok()】——分割字符串
查看>>
Linux下安装rabbitmq
查看>>
曹德旺
查看>>
【转】判断点在多边形内(matlab)
查看>>
java基础之集合:List Set Map的概述以及使用场景
查看>>
Python 线程 进程 协程
查看>>
iOS语言中的KVO机制
查看>>