Java
/
Spring Boot
- 1 Basics 9
-
Classes S
-
Objects S
-
Arrays S
-
Variables S
-
Loops S
-
Numbers S
-
Strings S
-
Exceptions S
-
Regexp S
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static S
-
Abstract
-
Interfaces
-
Constructors S
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text S
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable S
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread S
-
Sleep
-
Lock
-
Scheduler
- 6 Design Patterns 4
-
Singleton
-
Observer
-
Strategy
-
Mediator
- 7 Swing 12
-
Frame
-
Panel
-
Listener
-
Combo Box
-
Label
-
Image
-
Menu
-
Table
-
Layout
-
Drawing
-
Timer
-
Designer
- 8 I/O 7
-
Streams IO
-
Socket
-
Watching Files
-
Mail
-
Logger
-
Clipboard
-
Encrypt S
- 9 Effective 7
-
Constructors S
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static S
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit S
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions S
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods S
-
Single Responsibility
- 12 JavaFX 6
-
Openjfx
-
Scene Builder
-
First App
-
Jar Archive
-
On Action
-
Change Listener
- 13 Maven 4
-
Demo
-
Spring Boot
-
Junit
-
Guava
- 14 Spring Boot 13
-
Quick start S
-
Rest service S
-
Consuming rest S
-
Templates S
-
Security auth S
-
Command line S
-
Scheduled task S
-
Ajax S
-
Jdbc mysql S
-
Encrypt password S
-
Https S
-
Jwt S
-
Post request S
S
R
Q
Java Spring Boot Rest Service
spring-boot-starter-web @GetMapping("/greeting")
Dependencies
Simple RESTful web service with Spring on port 9090.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
Application
The service will handle GET requests and return JSON response.
/**
* Rest Controller service
*
* Use start.spring.io to create a new Spring Web project.
* The service will handle GET requests for /greeting and return JSON response.
*
* Use start.spring.io to create a new Spring Web project.
*/
package com.minte9.rest_service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Resource
In Spring RESTful web services, HTTP requests are handled by a {{resource controller`
/**
* Resource Controller - GreetingController
*
* HTTP requests are handled by a controller
* Components are identified by the @RestController annotation.
*/
package com.minte9.rest_service;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class QuoteController {
@GetMapping("/")
public String quote() {
return "Welcome! <br><br>Use /quote/{id}";
}
@GetMapping("/quote/{id}")
public Quote getQuote(@PathVariable Integer id) {
List<Quote> quotes = Arrays.asList(
new Quote(1, "Spring Boot makes stand alone Spring apps easy."),
new Quote(2, "With Boot you deploy everywhere.")
);
quotes = quotes.stream()
.filter(x -> x.getId() == id)
.collect(Collectors.toList());
return quotes.get(0);
}
}
Model
To model the quote representation, create a resource representation class.
/**
* Resource Representation Class - Greeting
*/
package com.minte9.rest_service;
class Quote {
private final Integer id;
private final String content;
public Quote(Integer id, String content) {
this.id = id;
this.content = content;
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
}
Port
You might want to set a different port from 8080 for you application.
# src/main/resources/application.properties
server.port=9090
Run
We can now build adn run the application and test the service.
mvn spring-boot:run
http://localhost:9090/quote/1 # {"id":1,"content":"Spring Boot ...
http://localhost:9090/quote/2 # {"id":2,"content":"With Boot you ...
➥ Questions
Last update: 14 days ago