Skip to content

Commit 5730335

Browse files
adding spring boot basic
1 parent 286a06f commit 5730335

File tree

7 files changed

+445
-0
lines changed

7 files changed

+445
-0
lines changed

SpringBootBasicExample/doc/help.txt

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
To Execute the Application
3+
---------------------------
4+
>mvn spring-boot:run
5+
6+
To get the output
7+
-------------------------
8+
url http://localhost:8080/application/welcome
9+
10+
To exit from Execution
11+
--------------------------
12+
ctrl-c
13+
14+
15+
===========================================================================
16+
To Create Executable Jar
17+
------------------------------------
18+
In Pom.xml file
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-maven-plugin</artifactId>
24+
</plugin>
25+
</plugins>
26+
</build>
27+
28+
> mvn package
29+
30+
To Check the content of jar file
31+
>jar tvf target/SpringBootBasicExample.jar
32+
33+
To execute the jar file
34+
>java -jar target/SpringBootBasicExample.jar
35+
36+
or create a jar and execute a file
37+
>mvn package && java -jar target/SpringBootBasicExample.jar

SpringBootBasicExample/pom.xml

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
7+
<modelVersion>4.0.0</modelVersion>
8+
9+
<groupId>SpringBootBasicExample</groupId>
10+
<artifactId>SpringBootBasicExample</artifactId>
11+
<version>1.0-SNAPSHOT</version>
12+
13+
<parent>
14+
<groupId>org.springframework.boot</groupId>
15+
<artifactId>spring-boot-starter-parent</artifactId>
16+
<version>1.3.1.RELEASE</version>
17+
</parent>
18+
19+
<organization>
20+
<name>ESpark</name>
21+
<url>http://adarshkumarsingh83.blogspot.in/</url>
22+
</organization>
23+
24+
<licenses>
25+
<license>
26+
<name>ESpark</name>
27+
<url>http://adarshkumarsingh83.blogspot.in/licenses/LICENSE-2.0.txt</url>
28+
<distribution>repo</distribution>
29+
</license>
30+
</licenses>
31+
32+
<developers>
33+
<developer>
34+
<id>adarshkumarsingh83</id>
35+
<name>Adarsh Kumar</name>
36+
<email>adarshkumarsingh83@gmail.com</email>
37+
<roles>
38+
<role>project architect</role>
39+
</roles>
40+
</developer>
41+
</developers>
42+
43+
<repositories>
44+
<repository>
45+
<id>maven2-repository.java.net</id>
46+
<name>Java.net Repository for Maven</name>
47+
<url>http://download.java.net/maven/2/</url>
48+
</repository>
49+
50+
<repository>
51+
<id>JBoss repository</id>
52+
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
53+
</repository>
54+
55+
<repository>
56+
<id>java.net</id>
57+
<url>https://maven.java.net/content/repositories/public/</url>
58+
</repository>
59+
60+
</repositories>
61+
62+
<properties>
63+
<project.name>SpringBootBasicExample</project.name>
64+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
65+
<java.version>1.7</java.version>
66+
<junit.version>4.11</junit.version>
67+
<slf4j.version>1.6.1</slf4j.version>
68+
<log4j.version>1.2.17</log4j.version>
69+
</properties>
70+
71+
<dependencies>
72+
73+
<dependency>
74+
<groupId>org.springframework.boot</groupId>
75+
<artifactId>spring-boot-starter-web</artifactId>
76+
<version>1.3.1.RELEASE</version>
77+
</dependency>
78+
79+
<dependency>
80+
<groupId>com.google.code.gson</groupId>
81+
<artifactId>gson</artifactId>
82+
<version>2.5</version>
83+
</dependency>
84+
85+
<dependency>
86+
<groupId>org.springframework.boot</groupId>
87+
<artifactId>spring-boot-starter-test</artifactId>
88+
<version>1.2.6.RELEASE</version>
89+
</dependency>
90+
91+
<dependency>
92+
<groupId>junit</groupId>
93+
<artifactId>junit</artifactId>
94+
<version>${junit.version}</version>
95+
</dependency>
96+
97+
<dependency>
98+
<groupId>org.slf4j</groupId>
99+
<artifactId>slf4j-api</artifactId>
100+
<version>${slf4j.version}</version>
101+
</dependency>
102+
103+
<dependency>
104+
<groupId>log4j</groupId>
105+
<artifactId>log4j</artifactId>
106+
<version>${log4j.version}</version>
107+
</dependency>
108+
109+
</dependencies>
110+
111+
<build>
112+
<finalName>${project.name}</finalName>
113+
<outputDirectory>target/classes</outputDirectory>
114+
<resources>
115+
<resource>
116+
<directory>src/main/resources</directory>
117+
<filtering>true</filtering>
118+
</resource>
119+
</resources>
120+
<plugins>
121+
<plugin>
122+
<artifactId>maven-compiler-plugin</artifactId>
123+
<configuration>
124+
<source>${java.version}</source>
125+
<target>${java.version}</target>
126+
<encoding>${project.build.sourceEncoding}</encoding>
127+
</configuration>
128+
</plugin>
129+
130+
<plugin>
131+
<groupId>org.springframework.boot</groupId>
132+
<artifactId>spring-boot-maven-plugin</artifactId>
133+
</plugin>
134+
</plugins>
135+
</build>
136+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2015 Espark And ©Adarsh Development Services @copyright All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* - Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* - Neither the name of Espark nor the names of its
16+
* contributors may be used to endorse or promote products derived
17+
* from this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20+
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
package com.espark.adarsh;
32+
33+
import org.springframework.beans.factory.annotation.Value;
34+
import org.springframework.boot.*;
35+
import org.springframework.boot.autoconfigure.*;
36+
import org.springframework.context.annotation.PropertySource;
37+
import org.springframework.web.bind.annotation.*;
38+
39+
import java.util.HashMap;
40+
import java.util.Map;
41+
42+
/**
43+
* @author Adarsh Kumar
44+
* @author $LastChangedBy: Adarsh Kumar$
45+
* @version $Revision: 0001 $, $Date:: 1/1/10 0:00 AM#$
46+
* @Espark @copyright all right reserve
47+
*/
48+
@RestController
49+
@EnableAutoConfiguration
50+
@RequestMapping("/application")
51+
@PropertySource("classpath:application.properties")
52+
public class SpringBootController {
53+
54+
@Value("${message}")
55+
private String message;
56+
57+
@RequestMapping(value = "/welcome"
58+
,produces = {"application/json"}
59+
,method = RequestMethod.GET)
60+
public
61+
@ResponseBody
62+
Map<String,String> welcomeMessage() {
63+
final Map<String,String> msg=new HashMap<String,String>(){
64+
{
65+
put("name",System.getProperty("user.name")) ;
66+
put("msg", "Hello " + System.getProperty("user.name") + " " +message) ;
67+
}
68+
};
69+
return msg;
70+
}
71+
72+
public static void main(String[] args) throws Exception {
73+
SpringApplication.run(SpringBootController.class, args);
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
server.port=8080
2+
message=Welcome To Spring Boot
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
3+
<log4j:configuration debug="true"
4+
xmlns:log4j='http://jakarta.apache.org/log4j/'>
5+
6+
<appender name="console" class="org.apache.log4j.ConsoleAppender">
7+
<layout class="org.apache.log4j.PatternLayout">
8+
<param name="ConversionPattern"
9+
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
10+
</layout>
11+
</appender>
12+
13+
<appender name="file" class="org.apache.log4j.RollingFileAppender">
14+
<param name="append" value="false" />
15+
<param name="maxFileSize" value="10MB" />
16+
<param name="maxBackupIndex" value="10" />
17+
<param name="file" value="/log/application.log" />
18+
<layout class="org.apache.log4j.PatternLayout">
19+
<param name="ConversionPattern"
20+
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
21+
</layout>
22+
</appender>
23+
24+
<root>
25+
<level value="ALL" />
26+
<appender-ref ref="console" />
27+
<appender-ref ref="file" />
28+
</root>
29+
30+
</log4j:configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (c) 2015 Espark And ©Adarsh Development Services @copyright All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* - Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* - Neither the name of Espark nor the names of its
16+
* contributors may be used to endorse or promote products derived
17+
* from this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20+
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
package com.espark.adarsh;
32+
33+
import static org.hamcrest.Matchers.equalTo;
34+
import static org.junit.Assert.assertThat;
35+
36+
import java.net.URL;
37+
import java.util.HashMap;
38+
import java.util.Map;
39+
40+
import com.google.gson.Gson;
41+
import com.google.gson.reflect.TypeToken;
42+
import org.junit.Before;
43+
import org.junit.Test;
44+
import org.junit.runner.RunWith;
45+
import org.springframework.beans.factory.annotation.Value;
46+
import org.springframework.boot.test.IntegrationTest;
47+
import org.springframework.boot.test.SpringApplicationConfiguration;
48+
import org.springframework.boot.test.TestRestTemplate;
49+
import org.springframework.http.ResponseEntity;
50+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
51+
import org.springframework.test.context.web.WebAppConfiguration;
52+
import org.springframework.web.client.RestTemplate;
53+
54+
55+
/**
56+
* @author Adarsh Kumar
57+
* @author $LastChangedBy: Adarsh Kumar$
58+
* @version $Revision: 0001 $, $Date:: 1/1/10 0:00 AM#$
59+
* @Espark @copyright all right reserve
60+
*/
61+
@RunWith(SpringJUnit4ClassRunner.class)
62+
@SpringApplicationConfiguration(classes = SpringBootController.class)
63+
@WebAppConfiguration
64+
@IntegrationTest({"server.port=0"})
65+
public class SpringBootControllerRestClientTest {
66+
67+
@Value("${local.server.port}")
68+
private int port;
69+
70+
@Value("${message}")
71+
private String message;
72+
73+
private URL base;
74+
private RestTemplate template;
75+
76+
@Before
77+
public void setUp() throws Exception {
78+
this.base = new URL("http://localhost:" + port + "/application/welcome");
79+
template = new TestRestTemplate();
80+
}
81+
82+
@Test
83+
public void getHello() throws Exception {
84+
ResponseEntity<String> response = template.getForEntity(base.toString(), String.class);
85+
final String jsonString=response.getBody();
86+
Map<String, String> retMap = new Gson().fromJson(jsonString, new TypeToken<HashMap<String, String>>() {}.getType());
87+
assertThat(retMap.get("name"), equalTo(System.getProperty("user.name")));
88+
assertThat(retMap.get("msg"), equalTo("Hello " + System.getProperty("user.name") + " " +message));
89+
}
90+
}

0 commit comments

Comments
 (0)