- OAuth 2.0 Cookbook
- Adolfo Eloy Nascimento
- 693字
- 2021-07-08 09:34:59
How to do it...
The following steps describe how to prepare the environment and show how to generate a simple project from the Spring Initializr website which will be executed using the appropriate Maven commands:
- Generate a project using Spring Initializr service by visiting https://start.spring.io/. Spring Initializr provides lots of options to start setting up your project, such as if you want to use Maven or Gradle to manage your project dependencies, which version of Spring Boot to use, which dependencies to use, and even changing the language from Java to Groovy or Kotlin.
- For this simple test, just use the default values for the project manager, Maven Project, with Java language and version 1.5.7 of the Spring Boot.
- At Project Metadata, change the value of the field Group to com.packt.example.
- Still on Project Metadata, change the name of the Artifact to simplemvc.
- In the Dependencies section, type web and select Full-stack web development with Tomcat and Spring MVC. After selecting the right choice, you will see the tag Web underneath Selected Dependencies as follows:

- After setting up all the requirements for this simple example, click on the Generate Project button and your browser will start downloading the ZIP file into your Downloads folder.
- After downloading this file, you can unzip it and import it to your IDE just to explore the structure of the created project. For Eclipse users, just import the project as a Maven project.
- Open the class SimplemvcApplication and you would see the following code in your IDE:
@SpringBootApplication public class SimplemvcApplication { public static void main(String[] args) { SpringApplication.run(SimplemvcApplication.class, args); } }
- Let's turn the class SimplemvcApplication into a controller by adding the annotation @Controller as presented in the following code:
@Controller @SpringBootApplication public class SimplemvcApplication { public static void main(String[] args) { SpringApplication.run(SimplemvcApplication.class, args); } }
- Now that our class is declared as a controller, we can define an endpoint so we can see if the project is running properly. Add the method getMessage as follows, within the class SimplemvcApplication:
@GetMapping("/message") public ResponseEntity<String> getMessage() { return ResponseEntity.ok("Hello!"); }
- If you want to run your project inside the Eclipse IDE, you should just run the class SimplemvcApplication as a Java application by right-clicking at the class and selecting the menu option Run As | Java Application.
- After the application is started you should see something like the following message at the end of the output presented in your console:
Started SimplemvcApplication in 13.558 seconds (JVM running for 14.011)
- Execute the following command to know if your application works properly (just check if the output prints Hello):
curl "http://localhost:8080/message"
- If you would like to use the command line you can also start your application by running the following Maven command (to run the application with Maven through the command line, you must install Maven, as explained in the next sections):
mvn spring-boot:run
- If you don't have Maven installed on your machine, the first thing to do is to start downloading the latest version from https://maven.apache.org/download.cgi which at the time of this writing was apache-maven-3.5.0-bin.tar.gz.
- After the file has downloaded, just unpack it into any folder you want and start running Maven commands.
- Copy the full path of the Maven directory, which was created when you unpacked the downloaded file from the Maven website. If you are running macOS or Linux, run pwd at the command line to discover the full path.
- After that, you must add the path for Maven's directory to the PATH environment variable. If you are using Linux or macOS, create the variable MVN_HOME within the .bash_profile file and append the content of MVN_HOME to the end of the PATH environment variable, as presented in the following code:
MVN_HOME=/Users/{your_user_name}/maven-3.5.0 export PATH=$PATH:$MVN_HOME/bin
The file .bash_profile should be found at the user's directory. So, to edit this file, you should open the file /Users/{your_user_name}/.bash_profile, or in a shorter way, by using ~/.bash_profile . If you are using Windows, all the environment variables can be edited through the visual interface.
- After editing this file, run the command source ~/.bash_profile to reload all the contents.
- To check if Maven is perfectly running on your environment, run the following command:
mvn --version.
推薦閱讀
- 案例式C語言程序設(shè)計(jì)
- Java編程指南:基礎(chǔ)知識(shí)、類庫應(yīng)用及案例設(shè)計(jì)
- 用Flutter極速構(gòu)建原生應(yīng)用
- SSM輕量級(jí)框架應(yīng)用實(shí)戰(zhàn)
- Python算法從菜鳥到達(dá)人
- 數(shù)據(jù)結(jié)構(gòu)習(xí)題解析與實(shí)驗(yàn)指導(dǎo)
- Getting Started with React Native
- C++寶典
- Python項(xiàng)目實(shí)戰(zhàn)從入門到精通
- Offer來了:Java面試核心知識(shí)點(diǎn)精講(框架篇)
- VMware vSphere 5.5 Cookbook
- ASP.NET本質(zhì)論
- 三步學(xué)Python
- 算法(第4版)
- 高性能Java架構(gòu):核心原理與案例實(shí)戰(zhàn)