Springboot (b): Set Springboot the same interface program start entry

Source: Internet
Author: User

Based on the previous article, a springboot simple project was built, in which the Hellowordcontroller.java interface class was written, and a main function was written in the class to start the portal for the interface service of the class. If you add more than one interface service class at this point, not modifying any code is an interface service that cannot access the new class.

In fact, Springboot provides a starting method for configuring the Global Scan Interface service class uniformly, this article describes how to use:

How to configure the Unified boot Portal

Create a new app package under the SRC root directory, creating a App.java class under the package that is configured as a generic Startup entry class for the project by code:

 Packageapp;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.EnableAutoConfiguration;ImportOrg.springframework.context.annotation.ComponentScan; @ComponentScan ("Com.dx.controller") @EnableAutoConfiguration Public classApp { Public Static voidMain (string[] args) {Springapplication.run (App.class, args); }}

Note:

1) Componentscan is used to configure the path of the package currently in the scan class;

2) enableautoconfiguration automatic injection;

3) Springapplication.run (App.class,args) is used as the server boot portal to start the interface service.

Test the interface service class:

The interface service classes already owned are:

Hellowordcontroller.java

 PackageCom.dx.controller;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod; @Controller Public classHellowordcontroller {@RequestMapping (value= "/index", method =requestmethod.get) PublicString Index () {SYSTEM.OUT.PRINTLN ("Index is running ..."); return"Welcome"; }}

Restcontrollertest.java

 PackageCom.dx.controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController;ImportJava.util.HashMap;ImportJava.util.Map; @RestController Public classrestcontrollertest {@RequestMapping ("/rest")     PublicMap<string, object>Rest () {Map<string, object> result =NewHashmap<>(); Result.put ("Code", "404"); Result.put ("MSG", "Unkown"); returnresult; }}

, run the app. App.java class, wait for startup to complete.

1) Visit: http://127.0.0.1:8888/rest

2) Access:



Springboot (b): Set Springboot the same interface program start entry

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.