Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build Spring Boot2

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to build Spring Boot2". In daily operation, I believe many people have doubts about how to build Spring Boot2. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to build Spring Boot2". Next, please follow the editor to study!

Environmental dependence

Spring Boot 2 requires at least Java 8 or 9 and Spring Framework 5.0.5.RELEASE or later. The version of Maven is 3.2 and the version of gradle is 4.

Servlet containers include:

Tomcat 8.5, Jetty 9.4, and Spring Boot applications can also be deployed to any container that is compatible with Servlet 3.1 +.

Construction

You can use Spring Boot as you would any standard Java library.

Use maven to build and confirm the version of java and maven.

Create a project and add web dependencies.

The following configurations are added to pom.xml:

Org.springframework.bootspring-boot-starter-parent2.0.1.RELEASE

And

Org.springframework.bootspring-boot-starter-web New controller

Create a new class Example with the following code:

/ * @ ClassName Example * @ Description spring boot 2 example * @ author zhangfeng * @ Date on April 25, 2018 10:53:24 * @ version 1.0.0 * / @ RestController@EnableAutoConfigurationpublic class Example {@ RequestMapping ("/") String home () {return "Hello World!";}}

The @ RestController annotation is equivalent to the combination of @ ResponseBody + @ Controller. If you just annotate Controller with @ RestController, the method in Controller cannot return the jsp page, the configured view parser InternalResourceViewResolver does not work, and the content returned is the content in Return.

The @ EnableAutoConfiguration comment tells SpringBoot to "guess" how you want to configure Spring, based on the fact that you have added jar dependencies. If Tomcat and Spring MVC have been added to spring-boot-starter-web, this comment automatically assumes that you are developing a web application and adding the appropriate spring settings.

Autoconfiguration is designed to work better with "Starters", but the two concepts are not directly related. You are free to pick and choose jar packages other than starter dependencies, and springboot will still try to configure your application automatically.

Spring usually recommends that we put the class of the main method under a root package, and the @ EnableAutoConfiguration (enable automatic configuration) annotation is usually placed on top of the class where main resides. The following is a typical structural layout:

Com +-example +-myproject +-Application.java | +-domain | +-Customer.java | +-CustomerRepository.java | +-service | +-CustomerService.java | +-web +-CustomerController.java

This allows @ EnableAutoConfiguration to search for annotated classes layer by layer. For example, if you are writing a JPA program (if you have configured it in your pom), spring will automatically search for @ Entity annotated classes and call them.

Start the application test

Run Firstspringboot2Application, then open a browser and type http://localhost:8080

You can see the browser returning Hello World!.

At this point, the study on "how to build Spring Boot2" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report