Skip to content
Snippets Groups Projects
Commit 05263965 authored by Joel Cavat's avatar Joel Cavat
Browse files

First sample

parents
No related branches found
No related tags found
No related merge requests found
# Summary
This simple example lets you run a http server with Spring boot.
Here are good resources who inspired this current snippet :
- https://spring.io/guides/gs/rest-service/
- https://www.baeldung.com/spring-controller-vs-restcontroller
## How to
Build and run :
```
mvn clean package
mvn spring-boot:run
```
Open a browser at : localhost:8080/toto
result:
```
{
"owner": "Marcus Miller",
"id": 2,
"xs": [
"Houhou",
"Haha"
]
}
```
pom.xml 0 → 100644
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.hepia</groupId>
<artifactId>http-server-example</artifactId>
<version>0.1.0</version>
<!--
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
-->
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.1.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.1.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
package ch.hepia;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/*
* DTO for Data Transrer Object
* This is not a Domain Object, just a reprensentation of something transferable.
* json-marshaller convert automatically to JSON. All getters will be translate
* to a JSON field
*
* Don't use DTO for your business model
*/
class DtoToto {
private final int i;
private final String owner;
public DtoToto(int i, String owner) {
this.i = i;
this.owner = owner;
}
public int getId(){ return this.i; }
public String getOwner(){ return this.owner; }
public List<String> getXs() { return List.of("Houhou", "Haha"); }
}
@SpringBootApplication
@RestController
public class App {
@RequestMapping("/toto")
public DtoToto greeting(@RequestParam(value="name", defaultValue="World") String name) {
return new DtoToto(2, "Marcus Miller");
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment