All Projects → spring-guides → Gs Rest Service

spring-guides / Gs Rest Service

Licence: apache-2.0
Building a RESTful Web Service :: Learn how to create a RESTful web service with Spring.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Gs Rest Service

Spring Cloud Stream Tutorial
Spring Cloud Stream Tutorial
Stars: ✭ 30 (-97.03%)
Mutual labels:  spring-boot
Spring Cloud Learning
☁️适合刚接触微服务架构的初学者了解和学习微服务,主要集成了注册中心、服务网关、配置中心、熔断机制、应用监控。
Stars: ✭ 37 (-96.34%)
Mutual labels:  spring-boot
Mastering Microservices With Java Third Edition
Mastering Microservices with Java – Third Edition, published by Packt
Stars: ✭ 39 (-96.14%)
Mutual labels:  spring-boot
Jbone
jbone基于Spring Cloud框架开发,旨在为中小企业提供稳定的微服务解决方案,为开发人员提供基础开发骨架,jbone包含微服务中所有常用组件,例如注册中心、服务管理、服务监控、JVM监控、内存分析、调用链跟踪、API网关等等。业务功能包括系统权限的统一管理、单点登录、CMS、电商平台、工作流平台、支付平台等等。
Stars: ✭ 961 (-4.95%)
Mutual labels:  spring-boot
Devops Service
DevOps Service is the core service of Choerodon. It integrated several open source tools to automate the DevOps process of planning, coding, building, testing, and deployment, operation, monitoring.
Stars: ✭ 36 (-96.44%)
Mutual labels:  spring-boot
Spring Boot
spring-boot 项目实践总结
Stars: ✭ 989 (-2.18%)
Mutual labels:  spring-boot
Web Framework For Java
A seed project with spring boot for AngularJS, AngularJs Material, Thymeleaf, RESTful API, MySQL and admin panel based on AdminLTE.
Stars: ✭ 29 (-97.13%)
Mutual labels:  spring-boot
Botwall4j
A botwall for Java web applications
Stars: ✭ 41 (-95.94%)
Mutual labels:  spring-boot
Nixmash Blog
A Blog Engine built with Spring Boot
Stars: ✭ 36 (-96.44%)
Mutual labels:  spring-boot
Ruoyi
基于SpringBoot2.1的权限管理系统 易读易懂、界面简洁美观。 核心技术采用Spring、MyBatis、Shiro没有任何其它重度依赖。直接运行即可用
Stars: ✭ 991 (-1.98%)
Mutual labels:  spring-boot
K8s Helm Helmfile
Project which compares 3 approaches to deploy apps on Kubernetes cluster (using kubectl, helm & helmfile)
Stars: ✭ 34 (-96.64%)
Mutual labels:  spring-boot
Hsweb Framework
hsweb (haʊs wɛb) 是一个基于spring-boot 2.x开发 ,首个使用全响应式编程的企业级后台管理系统基础项目。
Stars: ✭ 7,724 (+664%)
Mutual labels:  spring-boot
Jpetstore 6 Vaadin Spring Boot
This project is an exercise to port the original JPetStore-6 to Vaadin 8 and Spring Boot.
Stars: ✭ 38 (-96.24%)
Mutual labels:  spring-boot
Pig
🚀 The best rbac web framework. base on Spring Boot 2.4、 Spring Cloud 2020、 OAuth2 . Thx Give a star
Stars: ✭ 958 (-5.24%)
Mutual labels:  spring-boot
Depli
A handsome JVM monitoring dashboard
Stars: ✭ 39 (-96.14%)
Mutual labels:  spring-boot
Springboot Dubbox
基于Spring-boot和dubbox的API接口和后台管理系统
Stars: ✭ 948 (-6.23%)
Mutual labels:  spring-boot
Gwt Boot Modules
GWT Boot: BOMs and POMs, Starters, Dependencies and Parent
Stars: ✭ 38 (-96.24%)
Mutual labels:  spring-boot
Mica Example
mica 演示项目
Stars: ✭ 42 (-95.85%)
Mutual labels:  spring-boot
Spring Cloud Gateway
An example of using Spring cloud gateway
Stars: ✭ 40 (-96.04%)
Mutual labels:  spring-boot
Spring Thrift Api Gateway
Gateway for Apache Thrift requests processing that is built on Spring Cloud stack
Stars: ✭ 38 (-96.24%)
Mutual labels:  spring-boot

:spring_version: current :toc: :project_id: gs-rest-service :icons: font :source-highlighter: prettify

This guide walks you through the process of creating a "Hello, World" RESTful web service with Spring.

== What You Will Build

You will build a service that will accept HTTP GET requests at http://localhost:8080/greeting.

It will respond with a JSON representation of a greeting, as the following listing shows:

==== [source,json]

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

====

You can customize the greeting with an optional name parameter in the query string, as the following listing shows:

==== [source,text]

http://localhost:8080/greeting?name=User

====

The name parameter value overrides the default value of World and is reflected in the response, as the following listing shows:

==== [source,json]

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

====

== What You Need

:java_version: 1.8 include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/prereq_editor_jdk_buildtools.adoc[]

include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/how_to_complete_this_guide.adoc[]

[[scratch]] == Starting with Spring Initialize

If you use Maven, visit the https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.4.3.RELEASE&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=rest-service&name=rest-service&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.rest-service&dependencies=web[Spring Initializr] to generate a new project with the required dependency (Spring Web).

The following listing shows the pom.xml file that is created when you choose Maven:

==== [source,xml]

include::initial/pom.xml[]

====

If you use Gradle, visit the https://start.spring.io/#!type=gradle-project&language=java&platformVersion=2.4.3.RELEASE&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=rest-service&name=rest-service&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.rest-service&dependencies=web[Spring Initializr] to generate a new project with the required dependency (Spring Web).

The following listing shows the build.gradle file that is created when you choose Gradle:

==== [source,text]

include::initial/build.gradle[]

====

=== Manual Initialization (optional)

If you want to initialize the project manually rather than use the links shown earlier, follow the steps given below:

. Navigate to https://start.spring.io. This service pulls in all the dependencies you need for an application and does most of the setup for you. . Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java. . Click Dependencies and select Spring Web. . Click Generate. . Download the resulting ZIP file, which is an archive of a web application that is configured with your choices.

NOTE: If your IDE has the Spring Initializr integration, you can complete this process from your IDE.

[[initial]] == Create a Resource Representation Class

Now that you have set up the project and build system, you can create your web service.

Begin the process by thinking about service interactions.

The service will handle GET requests for /greeting, optionally with a name parameter in the query string. The GET request should return a 200 OK response with JSON in the body that represents a greeting. It should resemble the following output:

==== [source,json]

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

====

The id field is a unique identifier for the greeting, and content is the textual representation of the greeting.

To model the greeting representation, create a resource representation class. To do so, provide a plain old Java object with fields, constructors, and accessors for the id and content data, as the following listing (from src/main/java/com/example/restservice/Greeting.java) shows:

[source,java]

include::complete/src/main/java/com/example/restservice/Greeting.java[]

==== NOTE: This application uses the https://github.com/FasterXML/jackson[Jackson JSON] library to automatically marshal instances of type Greeting into JSON. Jackson is included by default by the web starter.

== Create a Resource Controller

In Spring's approach to building RESTful web services, HTTP requests are handled by a controller. These components are identified by the https://docs.spring.io/spring/docs/{spring_version}/javadoc-api/org/springframework/web/bind/annotation/RestController.html[`@RestController`] annotation, and the GreetingController shown in the following listing (from src/main/java/com/example/restservice/GreetingController.java) handles GET requests for /greeting by returning a new instance of the Greeting class:

==== [source,java]

include::complete/src/main/java/com/example/restservice/GreetingController.java[]

====

This controller is concise and simple, but there is plenty going on under the hood. We break it down step by step.

The @GetMapping annotation ensures that HTTP GET requests to /greeting are mapped to the greeting() method.

NOTE: There are companion annotations for other HTTP verbs (e.g. @PostMapping for POST). There is also a @RequestMapping annotation that they all derive from, and can serve as a synonym (e.g. @RequestMapping(method=GET)).

@RequestParam binds the value of the query string parameter name into the name parameter of the greeting() method. If the name parameter is absent in the request, the defaultValue of World is used.

The implementation of the method body creates and returns a new Greeting object with id and content attributes based on the next value from the counter and formats the given name by using the greeting template.

A key difference between a traditional MVC controller and the RESTful web service controller shown earlier is the way that the HTTP response body is created. Rather than relying on a view technology to perform server-side rendering of the greeting data to HTML, this RESTful web service controller populates and returns a Greeting object. The object data will be written directly to the HTTP response as JSON.

This code uses Spring https://docs.spring.io/spring/docs/{spring_version}/javadoc-api/org/springframework/web/bind/annotation/RestController.html[`@RestController`] annotation, which marks the class as a controller where every method returns a domain object instead of a view. It is shorthand for including both @Controller and @ResponseBody.

The Greeting object must be converted to JSON. Thanks to Spring's HTTP message converter support, you need not do this conversion manually. Because https://github.com/FasterXML/jackson[Jackson 2] is on the classpath, Spring's https://docs.spring.io/spring/docs/{spring_version}/javadoc-api/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.html[`MappingJackson2HttpMessageConverter`] is automatically chosen to convert the Greeting instance to JSON.

include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/spring-boot-application-new-path.adoc[]

include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/build_an_executable_jar_subhead.adoc[]

include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/build_an_executable_jar_with_both.adoc[]

Logging output is displayed. The service should be up and running within a few seconds.

== Test the Service

Now that the service is up, visit http://localhost:8080/greeting, where you should see:

==== [source,json]

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

====

Provide a name query string parameter by visiting http://localhost:8080/greeting?name=User. Notice how the value of the content attribute changes from Hello, World! to Hello, User!, as the following listing shows:

==== [source,json]

{"id":2,"content":"Hello, User!"}

====

This change demonstrates that the @RequestParam arrangement in GreetingController is working as expected. The name parameter has been given a default value of World but can be explicitly overridden through the query string.

Notice also how the id attribute has changed from 1 to 2. This proves that you are working against the same GreetingController instance across multiple requests and that its counter field is being incremented on each call as expected.

== Summary

Congratulations! You have just developed a RESTful web service with Spring.

== See Also

The following guides may also be helpful:

include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/footer.adoc[]

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].