Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Chapter 1 ■ Your First Spring Application } url "http://repo.spring.io/libs-snapshot" url 'http://repo.spring.io/milestone' url 'http://repo.spring.io/libs-release' } Listing 1-5 shows that instead of adding the Spring Framework dependency, now you are using a spring-boot-starter that will actually help to wire up everything to run this application as a web application. Listing 1-6 shows your main class, the HelloWorldController; this class introduces new annotations that will help the Spring container know what to do and create the necessary collaboration classes and run it as a web application. Listing 1-6. HelloWorldController.java package com.apress.isf.spring; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @EnableAutoConfiguration public class HelloWorldController { @RequestMapping("/") @ResponseBody String getMessage() { return "Hello World!"; } } public static void main(String[] args) throws Exception { SpringApplication.run(HelloWorldController.class, args); } The @Controller annotation will mark your class as a web controller that has a @RequestMapping and a @ResponseBody. All this means is that when your web application is running, it will accept requests from the http://localhost:8080/ URL and you should get some response back, such as the famous “Hello World” message. Running the Spring Boot Application Run the HelloWorldClass with the following command: gradle - run -DmainClass=com.apress.isf.spring.HelloWorldController 9

Chapter 1 ■ Your First Spring Application You should have the following output after executing the gradle command: isf-book$ gradle run -DmainClass=com.apress.isf.spring.HelloWorldController :ch01:compileJava UP-TO-DATE :ch01:compileGroovy UP-TO-DATE :ch01:processResources UP-TO-DATE :ch01:classes UP-TO-DATE :ch01:run :: Spring Boot :: (v1.0.2.RELEASE) INFO 84872 --- [main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080 INFO 84872 --- [main] o.apache.catalina.core.StandardService : Starting service Tomcat INFO 84872 --- [main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.52 INFO 84872 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext INFO 84872 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2030 ms INFO 84872 --- [main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080/http INFO 84872 --- [main] c.a.isf.spring.HelloWorldController : Started HelloWorldController in 7.086 seconds (JVM running for 7.599) > Building 80% > :ch01:run Now you can go to any browser and type in the URL http://localhost:8080 (see Figure 1-3). Figure 1-3. Spring Boot Web Page 10

Chapter 1 ■ Your First <strong>Spring</strong> Application<br />

}<br />

url "http://repo.spring.io/libs-snapshot"<br />

url 'http://repo.spring.io/milestone'<br />

url 'http://repo.spring.io/libs-release'<br />

}<br />

Listing 1-5 shows that instead of adding the <strong>Spring</strong> <strong>Framework</strong> dependency, now you are using a<br />

spring-boot-starter that will actually help to wire up everything to run this application as a web application.<br />

Listing 1-6 shows your main class, the HelloWorldController; this class introduces new annotations that<br />

will help the <strong>Spring</strong> container know what to do and create the necessary collaboration classes and run it as a web<br />

application.<br />

Listing 1-6. HelloWorldController.java<br />

package com.apress.isf.spring;<br />

import org.springframework.boot.<strong>Spring</strong>Application;<br />

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;<br />

import org.springframework.stereotype.Controller;<br />

import org.springframework.web.bind.annotation.RequestMapping;<br />

import org.springframework.web.bind.annotation.ResponseBody;<br />

@Controller<br />

@EnableAutoConfiguration<br />

public class HelloWorldController {<br />

@RequestMapping("/")<br />

@ResponseBody<br />

String getMessage() {<br />

return "Hello World!";<br />

}<br />

}<br />

public static void main(String[] args) throws Exception {<br />

<strong>Spring</strong>Application.run(HelloWorldController.class, args);<br />

}<br />

The @Controller annotation will mark your class as a web controller that has a @RequestMapping and a<br />

@ResponseBody. All this means is that when your web application is running, it will accept requests from the<br />

http://localhost:8080/ URL and you should get some response back, such as the famous “Hello World” message.<br />

Running the <strong>Spring</strong> Boot Application<br />

Run the HelloWorldClass with the following command:<br />

gradle - run -DmainClass=com.apress.isf.spring.HelloWorldController<br />

9

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!