Saturday, March 4, 2017

Spring Boot starter web app

(courtesy of the excellent javabrains.io https://javabrains.io/courses/spring_bootquickstart/ )
Install Spring Tool Suite https://spring.io/tools/sts/all

Run STS.exe
Adjust your Maven setting.xml (Window, Preferences, Maven, User Settings)

new / Maven Project

edit the pom.xml :

<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>io.javabrains.springbootquickstart</groupId>
  <artifactId>course-api</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Java Brains Course API</name>
  <parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.5.0.RELEASE</version>
  </parent>
  
  <dependencies>
  <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  
  </dependencies>
  
  <properties>
   <java.version>1.8</java.version>
  </properties>
</project>



Create this main App class

package io.javabrains.springbootstarter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CourseApiApp {

 public static void main(String[] args) {
  SpringApplication.run(CourseApiApp.class, args);

 }

}


and these 2 controller classes

package io.javabrains.springbootstarter.hello;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
 @RequestMapping("/hello")
 public String sayHi() {
  return "Hi";
 }
 
 

}


package io.javabrains.springbootstarter.topic;

import java.util.Arrays;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TopicController {
 @RequestMapping("/topics")
 public List getAllTopics() {
  return Arrays.asList(
    new Topic("a", "aa", "aaa"),
    new Topic("b", "bb", "bbb")
    );
 } 

}




where Topic is

package io.javabrains.springbootstarter.topic;

public class Topic {
 private String id;
 private String name;
 private String description;
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getDescription() {
  return description;
 }
 public void setDescription(String description) {
  this.description = description;
 }
 public Topic(String id, String name, String description) {
  super();
  this.id = id;
  this.name = name;
  this.description = description;
 }
 
 public Topic() {
  
 }
 

}




Run the main App (this will start the embedded tomcat), enter in the browser this

http://localhost:8080/topics

and you get a great JSON response....

[{"id":"a","name":"aa","description":"aaa"},{"id":"b","name":"bb","description":"bbb"}]



No comments: