Sample SpringBoot Web App and Microservices With Spring MVC
Project Classes And Files:
Controller Class
/**
*
*/
package com.gnsmind.springBoot.controller;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.gnsmind.springBoot.exception.ErrorObject;
import com.gnsmind.springBoot.model.User;
import com.gnsmind.springBoot.constant.Constants;
/**
* @author DEVELOPMENT
*
*/
@EnableAutoConfiguration
@RestController
@ConfigurationProperties(prefix = "helloapp")
public class MainController {
final static Logger logger = Logger.getLogger(MainController.class);
Map<String, String> propMap;
ErrorObject errObj = new ErrorObject();
private String saying;
// Info WS
@RequestMapping(value = Constants.GET_ALL_INFO_URI, method = RequestMethod.GET)
public Object getAllPaymentInfo() {
try {
return new String(
"<html><title>Spring World</title><body style=\"background: #fffb09; font-weight: bold;\">Spring Boot Work Shop<br><hr>Hello it world</body></html>");
} catch (Exception e) {
errObj.setErrMsg(e.getMessage());
errObj.setErrCode("1");
errObj.setErrType("fatal");
logger.error(e.getMessage());
System.err.println(e.getMessage());
return errObj;
}
}
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView index() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView save(@ModelAttribute User user) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("user-data");
modelAndView.addObject("user", user);
return modelAndView;
}
@RequestMapping(value = "/getIP", method = RequestMethod.GET, produces = "text/plain")
public String getIP() throws UnknownHostException {
String hostname = null;
try {
hostname = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
hostname = "unknown";
}
return saying + " Spring Boot microservice as IP INFO : " + hostname;
}
public String getSaying() {
return saying;
}
public void setSaying(String saying) {
this.saying = saying;
}
}
Constants Class
/**
*
*/
package com.gnsmind.springBoot.constant;
/**
* @author NET
*
*/
public class Constants {
//WS URL's
public static final String CONTROLLER_URI = "/main";
public static final String GET_ALL_INFO_URI = CONTROLLER_URI + "/getAllinfo";
//Messages
public static final String MESSAGE_ERROR_CONFIG_FILE = "Config file is missing!!!";
public static final String CONFIG_FILE = "config.properties";
}
SpringBoot Application Runner Class
package com.gnsmind.springBoot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootWorkShopApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWorkShopApplication.class, args);
}
}
Error Class
/**
*
*/
package com.gnsmind.springBoot.exception;
/**
* @author NET
*
*/
public class ErrorObject {
private String errMsg;
private String errCode;
private String errType;
/**
* @return the errMsg
*/
public String getErrMsg() {
return errMsg;
}
/**
* @param errMsg the errMsg to set
*/
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
/**
* @return the errCode
*/
public String getErrCode() {
return errCode;
}
/**
* @param errCode the errCode to set
*/
public void setErrCode(String errCode) {
this.errCode = errCode;
}
/**
* @return the errType
*/
public String getErrType() {
return errType;
}
/**
* @param errType the errType to set
*/
public void setErrType(String errType) {
this.errType = errType;
}
}
User (Model) Class
package com.gnsmind.springBoot.model;
public class User {
String name;
String email;
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
}
}
POM File (Maven)
<?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.gnsmind</groupId>
<artifactId>springBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringBootWorkShop</name>
<description>WorkShop project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>1.5.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-junit -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-picocontainer -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/net.masterthought/cucumber-reporting -->
<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>3.13.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
#---------Control Panel For Languages---------------
helloapp.saying=Merhabalar
management.security.enabled=false
log4j.properties
log4j.rootLogger=ERROR, file, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file_info=org.apache.log4j.RollingFileAppender
log4j.appender.file_info.File=D:\Log Files\InfoLog_SpringBootWorkShop.log
log4j.appender.file_info.MaxFileSize=8MB
log4j.appender.file_info.MaxBackupIndex=3
log4j.appender.file_info.layout=org.apache.log4j.PatternLayout
log4j.appender.file_info.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss} %p (%F:%L) - %m%n
log4j.appender.file_info.Threshold=INFO
log4j.appender.file_error=org.apache.log4j.RollingFileAppender
log4j.appender.file_error.File=D:\Log Files\ErrorLog_SpringBootWorkShop.log
log4j.appender.file_error.MaxFileSize=8MB
log4j.appender.file_error.MaxBackupIndex=10
log4j.appender.file_error.layout=org.apache.log4j.PatternLayout
log4j.appender.file_error.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss} %p (%F:%L) - %m%n
log4j.appender.file_error.Threshold=ERROR
log4j.category.org.springframework.web=DEBUG
org.apache.tomcat.util.http.Parameters.level = SEVERE
log4j.debug=true
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Index Page</title>
</head>
<body>
<form action="/save" method="post">
<table>
<tr>
<td><label for="user-name">User Name</label></td>
<td><input type="text" name="name"></input></td>
</tr>
<tr>
<td><label for="email">Email</label></td>
<td><input type="text" name="email"></input></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit"></input></td>
</tr>
</table>
</form>
</body>
</html>
user-data.html
<!DOCTYPE html>
<html xmlns:th="http://thymeleaf.org">
<table>
<tr>
<td><h4>User Name: </h4></td>
<td><h4 th:text="${user.name}"></h4></td>
</tr>
<tr>
<td><h4>Email ID: </h4></td>
<td><h4 th:text="${user.email}"></h4></td>
</tr>
</table>
</html>
Project Structure
Screen Shots :
Main page
Creation Of Base Spring-Boot Project (For Eclipse)
File --> New --> Spring Starter Project (You need to select "web" check-box in the next window)**This will create a starter project with all spring-boot functionality without controller class! You need to add your controller class regarding to your needs.
Comments
Post a Comment