spring cloud开发Greenwich版:服务提供者(一)

微服务’ralab-nlp’

  • New mocroservice ‘ralab-nlp’ and add the Maven spring-boot-starter-web denpendency
  • Add @SpringBootApplication to the application class
  • Add HelloController

本文采用maven建立应用程序(服务提供者ralab-nlp),引入spring-web依赖,pom.xml配置如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ralab</groupId>
<artifactId>ralab-nlp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ralab-nlp</name>
<description>Demo project for Spring Boot</description>

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

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

<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>
</plugin>
</plugins>
</build>

</project>

自动生成RalabNlpApplication.java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.ralab.ralabnlp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

//(scanBasePackages = { "com.ralab.nlp.controller"})
@SpringBootApplication
@ComponentScan({"com.ralab.nlp.controller"})
public class RalabNlpApplication {

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

}

新建HelloController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.ralab.nlp.controller;


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

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

@RestController
@RequestMapping("/nlp")
public class HelloController {

@GetMapping("/api")
public String index() {
return "Greetings from Spring Boot!";
}
}

运行RalabNlpApplication

测试http://localhost:8080/nlp/api

坚持原创技术分享,您的支持是我前进的动力!