`
sillycat
  • 浏览: 2560648 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring Boot and RESTful API(1)Prepare ENV and Web Layer

    博客分类:
  • JAVA
 
阅读更多
Spring Boot and RESTful API(1)Prepare ENV and Web Layer

Follow this document
https://spring.io/guides/gs/actuator-service/
https://spring.io/guides/gs/consuming-rest-restjs/

Some Information Endpoint
http://localhost:8080/application/health
http://localhost:8080/application/info
http://localhost:8080/application/metrics
http://localhost:8080/application/trace

http://localhost:8080/api/books/1

List of the SQL
http://localhost:8080/application/flyway

Console of H2
http://localhost:8080/h2-console

Maven Flyway Plugin
https://flywaydb.org/getstarted/firststeps/maven
http://qinghua.github.io/flyway/

webflux cassandra
https://github.com/bijukunjummen/sample-webflux-annot-cassandra
https://dzone.com/articles/spring-webflux-first-steps

Prepare the ENV gradle
http://wiki.jikexueyuan.com/project/gradle/java-quickstart.html
install gradle
http://sillycat.iteye.com/blog/2090147
http://sillycat.iteye.com/blog/1074642
https://gradle.org/install#manually

I chose to Install that manually
>wget https://downloads.gradle.org/distributions/gradle-3.5-bin.zip

Directly Unzip the file and place in the working area, add the bin to the PATH
>gradle --version

------------------------------------------------------------
Gradle 3.5
------------------------------------------------------------

Build time:   2017-04-10 13:37:25 UTC
Revision:     b762622a185d59ce0cfc9cbc6ab5dd22469e18a6

Groovy:       2.4.10
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.8.0_121 (Oracle Corporation 25.121-b13)
OS:           Mac OS X 10.12.5 x86_64

>gradle build

Reactor Introduction
https://www.ibm.com/developerworks/cn/java/j-cn-with-reactor-response-encode/index.html

Flux  -     0 - N sequence of items.         sequence of items, sequence ends and sequence error.
               If message produce, subscribe will be called with these methods onNext(), onComplete(), onError()

Mono -   0 - 1 If we put 2 Mono Sequence together, that is Flux.

Create Flux Static Method
just()
fromArray()
fromInterable()
fromStream
empty()
error(Throwable error)

Create Mono Static Method
just()
empty()
error()

Important Operation
filter
Flux.range(1, 10).filter(i->i%2 ==0).subscribe(System.out::println);

window - buffer
Flux.range(1, 100).window(20).subscribe(System.out::println):
Flux.intervalMillis(100).windowMillis(1001).take(2).toStream().forEach(System.out::println);

zipWith  take  reduce reduceWith  merge  mergeSequential  flatMap flatMapSequential concatMap combineLatest

Inputer1 ——event —> Service Handler —> Dispatch — > Request Handler1
                                                                 —> Dispatch —> Request Handler2
Inputer2 —— event —>                          —> Dispatch —> Request Handler3

Set Up the WebFlux on HTTP REST Layer
We can have reactor in the DAO layer as well by the supporting from latest spring. But it is fine. It is easy to use that only on top of the controller layer.

My pom.xml is as follow:
<?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.j2c</groupId>
<artifactId>jobs-monitor-api</artifactId>
<version>1.0.0</version>
<name>Jobs Monitor API</name>
<description>spring boot/cassandra/solr/webflux</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M1</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<!-- spring-boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- tools -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- Test -->
<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>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>

Application Class to start the Web JobsMonitorAPIApplication.java
package com.sillycat.jobsmonitorapi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class JobsMonitorAPIApplication {

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

}

Sample Controller JobsController.java
package com.sillycat.jobsmonitorapi.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.j2c.jobsmonitorapi.dto.JobDto;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/api1.0/jobs")
public class JobController {

    @GetMapping(path = "/{id}", produces = MediaTypes.JSON_UTF_8)
    public Mono<JobDto> get(@PathVariable("id") Long id) {
        JobDto dto = new JobDto();
        dto.id = 1l;
        dto.description = "java, scala, python, groovy, golang";
        dto.status = "LIVE";
        dto.title = "fullstack";
        dto.url = "sillycat.ddns.net";
        return Mono.just(dto);
    }

    @PostMapping(produces = MediaTypes.JSON_UTF_8)
    public Mono<ResponseEntity<JobDto>> save(@RequestBody JobDto dto) {
        return Mono.just(new ResponseEntity<>(dto, HttpStatus.CREATED));
    }

    @PutMapping(path = "/{id}", produces = MediaTypes.JSON_UTF_8)
    public Mono<ResponseEntity<JobDto>> update(@PathVariable("id") Long id, @RequestBody JobDto dto) {
        if (id == 0) {
            return Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND));
        } else {
            return Mono.just(new ResponseEntity<>(dto, HttpStatus.CREATED));
        }
    }

    @DeleteMapping(path = "/{id}", produces = MediaTypes.JSON_UTF_8)
    public Mono<ResponseEntity<String>> delete(@PathVariable("id") Long id) {
        return Mono.just(new ResponseEntity<>(HttpStatus.ACCEPTED));
    }

    @GetMapping(produces = MediaTypes.JSON_UTF_8)
    public Flux<JobDto> load() {
        JobDto dto1 = new JobDto();
        dto1.id = 1l;
        dto1.description = "java, scala, python, groovy, golang";
        dto1.status = "LIVE";
        dto1.title = "fullstack";
        dto1.url = "sillycat.ddns.net";
        JobDto dto2 = new JobDto();
        dto2.id = 2l;
        dto2.description = "java, scala, python, groovy, golang";
        dto2.status = "LIVE";
        dto2.title = "fullstack";
        dto2.url = "sillycat.ddns.net";
        List<JobDto> list = new ArrayList<JobDto>();
        list.add(dto1);
        list.add(dto2);
        //return Flux.just(list.toArray(new JobDto[list.size()]));
        return Flux.fromArray(list.toArray(new JobDto[list.size()]));
    }
}


References:
http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#getting-started-first-application
bunch of samples
https://github.com/spring-projects/spring-boot/tree/v2.0.0.M1/spring-boot-samples
bean mapping
http://orika-mapper.github.io/orika-docs/intro.html
pom version
https://github.com/spring-projects/spring-boot/blob/d21a5076feb1ba24da3a6e2a0c72c8003cf3701f/spring-boot-dependencies/pom.xml
https://github.com/springside/springside4/wiki/Tutorial
分享到:
评论

相关推荐

    Spring Boot编写RESTful API.txt

    免费的学习视频,比网上骗人那些强多了,希望对大家有帮助。如果没有账号的注册一下就可以了

    Spring boot restful api demo

    总结,Spring Boot结合RESTful API设计原则,为开发者提供了快速构建高效、易于维护的Web服务的平台。这个"Spring Boot RESTful API demo"项目展示了如何创建、查询和管理资源的基本步骤。通过实践和扩展,可以实现...

    Spring Boot开发实战:基于Spring Boot的RESTful API服务的实验心得与案例解析

    ### Spring Boot开发实战:基于Spring Boot的RESTful API服务的实验心得与案例解析 #### 一、引言 Spring Boot自发布以来,以其强大的自动配置能力、简洁的开发模式以及丰富的社区支持,迅速成为了Java开发者构建...

    基于Java与Spring Boot的RESTful API设计与调用源码

    该项目提供基于Java和Spring Boot框架构建的RESTful API设计与调用源码,涵盖17个Java源文件、9个XML配置文件、6个JavaScript文件、4个CSS样式文件、2个HTML文件,共计46个文件。项目旨在实现Spring Boot的API发布与...

    SpringBoot+Mybatis+CXF框架,实现Restful api与 WebService api接口的大实验

    3. 接下来,我们需要实现Restful API和WebService API接口,使用Spring Boot的Restful API和CXF框架来实现学生信息的增删改查操作。 4. 最后,我们需要测试Restful API和WebService API接口,确保其正常工作。 结论...

    Spring Boot中使用Swagger2构建强大的RESTful API文档

    为了解决上面这样的问题,本文将介绍RESTful API的重磅好伙伴Swagger2,它可以轻松的整合到Spring Boot中,并与Spring MVC程序配合组织出强大RESTful API文档。它既可以减少我们创建文档的工作量,同时说明内容又...

    Maven Archetype与Spring Boot搭建RESTful API实例(包含详细的完整的程序和数据)

    本文主要介绍了通过使用 Maven Archetype 与 Spring Boot 能够迅速搭建起一个支持CRUD的基本RESTful API项目框架,并提供了从创建初始项目到完成简易数据操作(如增删查)的具体指南和代码样例。 适合具有一定经验但...

    一个基于 Spring Boot 的RESTful API项目示例:图书管理系统

    内容概要:本文详述了如何利用Spring Boot技术搭建一套简易图书管理系统的RESTful API接口流程,涵盖从初始创建到实现完整CRUD(增删改查)功能的整体步骤,并提供H2内存数据库作为数据存储支持。此外介绍了系统主要...

    Spring Boot + Mybatis 整合实现RESTful API

    Spring Boot 整合 Mybatis 实现RESTful API ,具体可以查看博客: http://blog.csdn.net/yaozhiqi1905658804/article/details/70820892

    Spring Boot-RESTfull API入门.rar

    1. **MVC 模式**:Spring Boot 基于 Spring MVC 框架提供了一种简洁的 Web 开发方式。你可以使用 `@RestController` 注解标记控制器类,而 `@RequestMapping` 和 `@GetMapping`、`@PostMapping` 等注解则用于映射...

    基于Spring Boot与Java的图书管理系统RESTful API实战

    内容概要:详细介绍使用Java、Spring Boot以及相关技术和工具如Maven、H2 database搭建简单的图书管理RESTful API系统全过程,覆盖了从项目的建立、实体类定义、接口编写一直到API的功能测试。项目主要提供了增加、...

    spring-boot-04-web-restfulcrud

    《Spring Boot 2.4 RESTful CRUD 实战详解》 在现代Web开发中,Spring Boot以其简洁、高效的特点,已经成为主流框架之一。本教程聚焦于Spring Boot 2.4版本,针对初学者提供一个完整的RESTful CRUD(创建、读取、...

    swagger整合Spring Boot生成Restful接口文档

    而Swagger是目前最流行的接口文档解决方案,本文主要通过代码实战的方式讲解Spring Boot 和Swagger集成生成Restful接口文档。教程参见 http://blog.csdn.net/zjx2016/article/details/74407832

    Spring Boot Swagger2 构建RESTful API

    在Spring Boot项目中集成Swagger2,可以帮助我们快速地构建和维护高质量的RESTful API。以下将详细讲解如何利用Spring Boot与Swagger2进行集成,并展示其主要功能和步骤。 **一、集成Swagger2** 1. 添加依赖:首先...

    spring boot restful 接口示例项目

    在这个“Spring Boot RESTful 接口示例项目”中,我们可以学习到如何使用 Spring Boot 创建 RESTful 风格的 API。RESTful API 通常通过 HTTP 协议提供服务,利用 GET、POST、PUT、DELETE 等方法操作资源,实现客户端...

    基于Spring Boot,采用RESTful风格架构的微信点餐系统源码(高分毕设).zip

    基于Spring Boot,采用RESTful风格架构的微信点餐系统源码(高分毕设).zip 基于Spring Boot,采用RESTful风格架构的微信点餐系统源码(高分毕设).zip 基于Spring Boot,采用RESTful风格架构的微信点餐系统源码...

    spring-boot-web-restfulcrud代码示例

    在“spring-boot-web-restfulcrud”这个项目中,我们关注的是如何使用 Spring Boot 构建一个基于 Web 的 RESTful CRUD(创建、读取、更新和删除)应用。RESTful 风格是一种软件架构风格,用于设计网络应用程序,通过...

    基于 Spring Boot 的 Restful 风格实现增删改查.docx

    基于 Spring Boot 的 Restful 风格实现增删改查

    spring boot 示例代码

    【Spring Boot 示例代码】是一个专为初学者设计的项目,旨在教授如何利用Spring Boot搭建RESTful API服务。Spring Boot是Spring框架的一个子项目,它简化了配置和启动过程,使得开发者能够快速创建独立运行的Java...

Global site tag (gtag.js) - Google Analytics