教程 > spring boot 中文教程 >
阅读:82
spring boot 构建 restful web 服务——迹忆客-ag捕鱼王app官网
spring boot 为企业应用构建 restful web services 提供了很好的支持。 本章将详细解释使用 spring boot 构建 restful web 服务。
注意
- 要构建 restful web 服务,我们需要将 spring boot starter web 依赖项添加到构建配置文件中。
如果你是 maven 用户,请使用以下代码在 pom.xml 文件中添加以下依赖项
org.springframework.boot
spring-boot-starter-web
如果你是 gradle 用户,请使用以下代码在 build.gradle 文件中添加以下依赖项。
compile('org.springframework.boot:spring-boot-starter-web')
完整构建配置文件 maven build – pom.xml 的代码如下
4.0.0
com.tutorialspoint
demo
0.0.1-snapshot
jar
demo
demo project for spring boot
org.springframework.boot
spring-boot-starter-parent
1.5.8.release
utf-8
utf-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
下面给出了完整的构建配置文件 gradle build – build.gradle 的代码
buildscript {
ext {
springbootversion = '1.5.8.release'
}
repositories {
mavencentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springbootversion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.study'
version = '0.0.1-snapshot'
sourcecompatibility = 1.8
repositories {
mavencentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testcompile('org.springframework.boot:spring-boot-starter-test')
}
在继续构建 restful web 服务之前,建议了解以下注解
rest controller
@restcontroller
注解用于定义 restful web 服务。 它提供 json、xml 和自定义响应。 它的语法如下所示
@restcontroller
public class productservicecontroller {
}
请求映射
@requestmapping
注解用于定义请求 uri 以访问 rest 端点。 我们可以定义 request 方法来消费和生产对象。 默认请求方法是 get。
@requestmapping(value = "/products")
public responseentity