官术网_书友最值得收藏!

Working with your favorite IDE

Now is the time to pick the IDE. Though there are many IDEs used for Spring Boot projects, I would recommend using Spring Tool Suite (STS), as it is open source and easy to manage projects with. In my case, I use sts-3.8.2.RELEASE. You can download the latest STS from this link: https://spring.io/tools/sts/all. In most cases, you may not need to install; just unzip the file and start using it:

After extracting the STS, you can start using the tool by running STS.exe (shown in the preceding screenshot).

In STS, you can import the project by selecting Existing Maven Projects, shown as follows:

After importing the project, you can see the project in Package Explorer, as shown in the following screenshot:

You can see the main Java file (TicketManagementApplication) by default:

To simplify the project, we will clean up the existing POM file and update the required dependencies. Add this file configuration to pom.xml:

<?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.packtpub.restapp</groupId>
<artifactId>ticket-management</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ticket-management</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.5.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.5.7.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>1.5.7.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

In the preceding configuration, you can check that we have used the following libraries:

  • spring-web
  • spring-boot-starter
  • spring-boot-starter-tomcat
  • spring-bind
  • jackson-databind

As the preceding dependencies are needed for the project to run, we have added them to our pom.xml file.

So far we have got the base project ready for Spring Web Service. Let's add a basic REST code to the application. First, remove the @SpringBootApplication annotation from the TicketManagementApplication class and add the following annotations:

@Configuration
@EnableAutoConfiguration
@ComponentScan
@Controller

These annotations will help the class to act as a web service class. I am not going to talk much about what these configurations will do in this chapter. After adding the annotations, please add a simple method to return a string as our basic web service method:

@ResponseBody
@RequestMapping("/")
public String sayAloha(){
return "Aloha";
}

Finally, your code will look as follows:

package com.packtpub.restapp.ticketmanagement;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Configuration
@EnableAutoConfiguration
@ComponentScan
@Controller
public class TicketManagementApplication {
@ResponseBody
@RequestMapping("/")
public String sayAloha(){
return "Aloha";
}
public static void main(String[] args) {
SpringApplication.run(TicketManagementApplication.class, args);
}
}

Once all the coding changes are done, just run the project on Spring Boot App (Run As | Spring Boot App). You can verify the application has loaded by checking this message in the console:

Tomcat started on port(s): 8080 (http)

Once verified, you can check the API on the browser by simply typing localhost:8080. Check out the following screenshot:

If you want to change the port number, you can configure a different port number in application.properties, which is in src/main/resources/application.properties. Check out the following screenshot:

主站蜘蛛池模板: 泰州市| 临武县| 上饶市| 镇赉县| 合川市| 天水市| 海门市| 大英县| 化隆| 浮山县| 普格县| 淳安县| 正阳县| 岳阳县| 潞西市| 措美县| 夏河县| 米林县| 陇川县| 宜都市| 罗源县| 尤溪县| 墨玉县| 白水县| 江津市| 新兴县| 商都县| 区。| 炎陵县| 江都市| 台东市| 长汀县| 城市| 荣昌县| 博客| 千阳县| 汽车| 小金县| 德州市| 卓尼县| 于田县|