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

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:

主站蜘蛛池模板: 比如县| 镇安县| 民勤县| 广宗县| 夏津县| 澄迈县| 德昌县| 兴海县| 汾西县| 固镇县| 同江市| 安新县| 湖州市| 娄底市| 潢川县| 黔江区| 上蔡县| 屏边| 绥滨县| 通化市| 乌拉特后旗| 万宁市| 罗江县| 盐源县| 乐平市| 乌拉特后旗| 天峻县| 荣成市| 青铜峡市| 西昌市| 木兰县| 普安县| 谢通门县| 松潘县| 临沂市| 陇南市| 丽水市| 晋宁县| 深泽县| 梁山县| 定西市|