angelcool@lolcahost:~/code-learning/Java/spring-boot$ date
Sun Jun 1 12:30:32 PM PDT 2025
angelcool@lolcahost:~/code-learning/Java/spring-boot$ pwd
/home/angelcool/code-learning/Java/spring-boot
# INITIALIZE APPLICATION
angelcool@lolcahost:~/code-learning/Java/spring-boot$ spring init -a my-demo -l java --build maven -x my-demo
Using service at https://start.spring.io
Project extracted to '/home/angelcool/code-learning/Java/spring-boot/my-demo'
angelcool@lolcahost:~/code-learning/Java/spring-boot$
# INSTALL MAVEN
angelcool@localhost:~/code-learning/Java/spring-boot/my-demo$ sudo dnf install maven
...
angelcool@localhost:~/code-learning/Java/spring-boot/my-demo$ mvn --version
Apache Maven 3.9.6 (Red Hat 3.9.6-7)
...
# RUN APPLICATION, NOTE: I think I had to update the artifactId in pom.xml to 'spring-boot-starter-web'
angelcool@localhost:~/code-learning/Java/spring-boot/my-demo$ mvn spring-boot:run
...
# MAKE REQUEST
angelcool@localhost:~/code-learning/Java/spring-boot/my-demo$ http http://localhost:8080/coffees
HTTP/1.1 200
Connection: keep-alive
Content-Type: application/json
Date: Mon, 02 Jun 2025 03:13:46 GMT
Keep-Alive: timeout=60
Transfer-Encoding: chunked
[
{
"id": "3d1256a7-c5c8-4a9b-9ef3-60da3107b06b",
"name": "Black"
},
{
"id": "7fc475b2-7c39-4412-baeb-6ac01f46128c",
"name": "French Vanilla"
},
{
"id": "690c16af-3931-4dbd-b573-a9d8c86e0a3c",
"name": "Hazelnut"
}
]
# SOURCE CODE
angelcool@localhost:~/code-learning/Java/spring-boot/my-demo$ cat src/main/java/com/example/my_demo/DemoApplication.java
package com.example.my_demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
// import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
// import org.springframework.ui.Model;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.UUID;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
class Coffee {
private final String id;
private String name;
public Coffee(String id, String name) {
this.id = id;
this.name = name;
}
public Coffee (String name) {
this(UUID.randomUUID().toString(),name);
}
public String getId(){
return this.id;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
}
@RestController
class RestApiController{
private List<Coffee> coffees = new ArrayList<>();
public RestApiController(){
this.coffees.addAll(List.of(
new Coffee("Black"),
new Coffee("French Vanilla"),
new Coffee("Hazelnut")
));
}
@RequestMapping(value = "/coffees", method = RequestMethod.GET)
Iterable<Coffee> getCoffees() {
return this.coffees;
}
}
[ view entry ] ( 20 views ) | print article
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Next> Last>>