<?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 >com.in28minutes.rest.webservices</groupId >
<artifactId >restful-web-services</artifactId >
<version >0.0.1-SNAPSHOT</version >
<packaging >jar</packaging >
<name >restful-web-services</name >
<description >Demo project for Spring Boot</description >
<parent >
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-starter-parent</artifactId >
<version >2.0.0.RELEASE</version >
<relativePath /> <!-- lookup parent from repository -->
</parent >
<properties >
<project .build.sourceEncoding>UTF-8</project .build.sourceEncoding>
<project .reporting.outputEncoding>UTF-8</project .reporting.outputEncoding>
<java .version>1.8</java .version>
</properties >
<dependencies >
<dependency >
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-starter-data-jpa</artifactId >
</dependency >
<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 >
</dependency >
<dependency >
<groupId >com.h2database</groupId >
<artifactId >h2</artifactId >
<scope >runtime</scope >
</dependency >
<dependency >
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-starter-test</artifactId >
<scope >test</scope >
</dependency >
</dependencies >
<build >
<plugins >
<plugin >
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-maven-plugin</artifactId >
</plugin >
</plugins >
</build >
<repositories >
<repository >
<id >spring-snapshots</id >
<name >Spring Snapshots</name >
<url >https://repo.spring.io/snapshot</url >
<snapshots >
<enabled >true</enabled >
</snapshots >
</repository >
<repository >
<id >spring-milestones</id >
<name >Spring Milestones</name >
<url >https://repo.spring.io/milestone</url >
<snapshots >
<enabled >false</enabled >
</snapshots >
</repository >
</repositories >
<pluginRepositories >
<pluginRepository >
<id >spring-snapshots</id >
<name >Spring Snapshots</name >
<url >https://repo.spring.io/snapshot</url >
<snapshots >
<enabled >true</enabled >
</snapshots >
</pluginRepository >
<pluginRepository >
<id >spring-milestones</id >
<name >Spring Milestones</name >
<url >https://repo.spring.io/milestone</url >
<snapshots >
<enabled >false</enabled >
</snapshots >
</pluginRepository >
</pluginRepositories >
</project >
/src/main/java/com/in28minutes/rest/webservices/restfulwebservices/helloworld/HelloWorldBean.java
package com .in28minutes .rest .webservices .restfulwebservices .helloworld ;
public class HelloWorldBean {
private String message ;
public HelloWorldBean (String message ) {
this .message = message ;
}
public String getMessage () {
return message ;
}
public void setMessage (String message ) {
this .message = message ;
}
@ Override
public String toString () {
return String .format ("HelloWorldBean [message=%s]" , message );
}
}
/src/main/java/com/in28minutes/rest/webservices/restfulwebservices/helloworld/HelloWorldController.java
package com .in28minutes .rest .webservices .restfulwebservices .helloworld ;
import org .springframework .web .bind .annotation .GetMapping ;
import org .springframework .web .bind .annotation .PathVariable ;
import org .springframework .web .bind .annotation .RestController ;
//Controller
@ RestController
public class HelloWorldController {
@ GetMapping (path = "/hello-world" )
public String helloWorld () {
return "Hello World" ;
}
@ GetMapping (path = "/hello-world-bean" )
public HelloWorldBean helloWorldBean () {
return new HelloWorldBean ("Hello World" );
}
///hello-world/path-variable/in28minutes
@ GetMapping (path = "/hello-world/path-variable/{name}" )
public HelloWorldBean helloWorldPathVariable (@ PathVariable String name ) {
return new HelloWorldBean (String .format ("Hello World, %s" , name ));
}
}
/src/main/java/com/in28minutes/rest/webservices/restfulwebservices/RestfulWebServicesApplication.java
package com .in28minutes .rest .webservices .restfulwebservices ;
import org .springframework .boot .SpringApplication ;
import org .springframework .boot .autoconfigure .SpringBootApplication ;
@ SpringBootApplication
public class RestfulWebServicesApplication {
public static void main (String [] args ) {
SpringApplication .run (RestfulWebServicesApplication .class , args );
}
}
/src/main/java/com/in28minutes/rest/webservices/restfulwebservices/user/User.java
package com .in28minutes .rest .webservices .restfulwebservices .user ;
import java .util .Date ;
public class User {
private Integer id ;
private String name ;
private Date birthDate ;
public User (Integer id , String name , Date birthDate ) {
super ();
this .id = id ;
this .name = name ;
this .birthDate = birthDate ;
}
public Integer getId () {
return id ;
}
public void setId (Integer id ) {
this .id = id ;
}
public String getName () {
return name ;
}
public void setName (String name ) {
this .name = name ;
}
public Date getBirthDate () {
return birthDate ;
}
public void setBirthDate (Date birthDate ) {
this .birthDate = birthDate ;
}
@ Override
public String toString () {
return String .format ("User [id=%s, name=%s, birthDate=%s]" , id , name , birthDate );
}
}
/src/main/java/com/in28minutes/rest/webservices/restfulwebservices/user/UserDaoService.java
package com .in28minutes .rest .webservices .restfulwebservices .user ;
import java .util .ArrayList ;
import java .util .Date ;
import java .util .List ;
import org .springframework .stereotype .Component ;
@ Component
public class UserDaoService {
private static List <User > users = new ArrayList <>();
private static int usersCount = 3 ;
static {
users .add (new User (1 , "Adam" , new Date ()));
users .add (new User (2 , "Eve" , new Date ()));
users .add (new User (3 , "Jack" , new Date ()));
}
public List <User > findAll () {
return users ;
}
public User save (User user ) {
if (user .getId () == null ) {
user .setId (++usersCount );
}
users .add (user );
return user ;
}
public User findOne (int id ) {
for (User user : users ) {
if (user .getId () == id ) {
return user ;
}
}
return null ;
}
}
/src/main/java/com/in28minutes/rest/webservices/restfulwebservices/user/UserResource.java
package com .in28minutes .rest .webservices .restfulwebservices .user ;
import java .util .List ;
import org .springframework .beans .factory .annotation .Autowired ;
import org .springframework .web .bind .annotation .GetMapping ;
import org .springframework .web .bind .annotation .PathVariable ;
import org .springframework .web .bind .annotation .RestController ;
@ RestController
public class UserResource {
@ Autowired
private UserDaoService service ;
@ GetMapping ("/users" )
public List <User > retrieveAllUsers () {
return service .findAll ();
}
@ GetMapping ("/users/{id}" )
public User retrieveUser (@ PathVariable int id ) {
return service .findOne (id );
}
}
/src/main/resources/application.properties
logging.level.org.springframework = info
# This is not really needed as this is the default after 2.0.0.RELEASE
spring.jackson.serialization.write-dates-as-timestamps =false
spring.messages.basename =messages
/src/test/java/com/in28minutes/rest/webservices/restfulwebservices/RestfulWebServicesApplicationTests.java
package com .in28minutes .rest .webservices .restfulwebservices ;
import org .junit .Test ;
import org .junit .runner .RunWith ;
import org .springframework .boot .test .context .SpringBootTest ;
import org .springframework .test .context .junit4 .SpringRunner ;
@ RunWith (SpringRunner .class )
@ SpringBootTest
public class RestfulWebServicesApplicationTests {
@ Test
public void contextLoads () {
}
}