Java
/
Spring boot
- 1 Basics 9
-
Classes
-
Objects
-
Arrays
-
Variables
-
Loops
-
Numbers
-
Strings
-
Exceptions
-
Regexp
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static
-
Abstract
-
Interfaces
-
Constructors
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread
-
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
- 9 Effective 7
-
Constructors
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods
-
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 8
-
Quick start
-
Rest service
-
Consuming Rest
-
Templates
-
Security
-
Command Line
-
Scheduling Tasks
-
Ajax
/
Rest service
➟
➟
Last update: 13-03-2022
Application
The service will handle GET requests for /greeting 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.demorest;
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.demorest;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
private final AtomicLong counter = new AtomicLong();
@GetMapping("/greeting")
public Greeting greeting(
@RequestParam(value = "name", defaultValue = "World") String name
) {
Long id = counter.incrementAndGet();
String content = String.format("Hello %s!", name);
return new Greeting(id, content);
}
}
Representation
To model the greeting representation, create a resource representation class.
/**
* Resource Representation Class - Greeting
*/
package com.minte9.demorest;
class Greeting {
private final long id;
private final String content;
public Greeting(long id, String content) {
this.id = id;
this.content = content;
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
}
Run
We can now build adn run the application and test the service.
./mvnw spring-boot:run
http://localhost:8080/greeting
# {"id":1,"content":"Hello World!"}
http://localhost:8080/greeting?name=User
# {"id":2,"content":"Hello User!"}
Port
You might want to set a different port from 8080 for you application.
# /restservice/src/main/resources/application.properties
server.port=9090
➥ Questions github Spring-boot