Start and call of Spring Boot

Keywords: Web Development Spring Lombok REST xml

[eye]

1. A way to start Spring Boot
Service startup class

package com.npc.rest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

/**
 * Frozen
 * 2020-1-29
 */

/**
 * Previously, users used three annotations to annotate their main class.
 * They are @ Configuration,@EnableAutoConfiguration,@ComponentScan.
 * Since these annotations are generally used together,
 * spring boot Provides a unified annotation @ SpringBootApplication.
 * @SpringBootApplication =
 * (Default property) @ Configuration + @EnableAutoConfiguration + @ComponentScan.
 * Explain @ Configuration,@EnableAutoConfiguration,@ComponentScan separately.
 *
 * 1,@Configuration: When it comes to @ Configuration, it's about his partner @ Bean.
 * Using these two annotations, you can create a simple spring configuration class, which can be used to replace the corresponding xml configuration file.
 * <beans>
 *     <bean id = "car" class="com.test.Frozen">
 *         <property name="red" ref = "red"></property>
 *     </bean>
 *     <bean id = "ant" class="com.test.RedAnt"></bean>
 * </beans>
 *  Amount to:
 * @Configuration
 * public class Conf {
 *     @Bean
 *     public Car car() {
 *         Frozen item = new Frozen();
 *         item.setRed(redant());
 *         return item;
 *     }
 *     @Bean
 *     public RedAnt redant() {
 *         return new RedAnt();
 *     }
 * }
 * @Configuration This class can use Spring
 * IoC The container is the source of the bean definition.
 * @Bean Annotation tells Spring that an annotation method with @ Bean will return an object,
 * The object should be registered as a bean in the Spring application context.
 * 2,@EnableAutoConfiguration: 
 * It can automatically configure the context of spring, try to guess and configure the bean class you want,
 * It is usually automatically configured based on your classpath and your bean definition.
 * 3,@ComponentScan: 
 * All classes marked @ Component under the specified package will be automatically scanned and registered as bean s,
 * Of course, it includes the sub annotations @ Service,@Repository,@Controller under @ Component.
 */
@SpringBootApplication
@ImportResource(value = {"classpath:css-npc-web.xml"})//Introduce other resources, plus or not depending on the situation
public class FrozenApplication {
        public static void main(String[] args) {
                SpringApplication.run(FrozenApplication.class, args);
        }
}

Method implementation class

package com.npc.rest;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * Frozen
 * 2020 17:03:31, January 29, 2015
 * Service implementation class
 */

/**
 * @RestController = @Controller + @ResponseBody Form,
 * Two comrades on the right of the equal sign simply introduce two sentences, and we will understand the meaning of @ RestController:
 * @Controller
 * Inject the currently decorated class into the SpringBoot IOC container,
 * This class will be instantiated during the process of running from the project where the class is located.
 * @ResponseBody Its function is simply to refer to the data returned by all API interfaces in this class,
 * Will be returned to the client as a Json string
 */
@RestController
@RequestMapping("/redant")
@Slf4j
public class FrozenTest {
        @GetMapping("/hoyl")
        public void genToken() {
                System.err.println("Time: January 29, 2020 16:58:15,test data");
                for (int i = 0; i < 10; i++) {
                        Date date = new Date();
                        long times = date.getTime();
                        System.err.println("Time test data:"+times);
                        log.info("Time test data:"+times);
                }
        }
}

[PS: I have something to say about @ Slf4j]
1. @ Slf4j what does this thing do?
Answer: if you don't want to write private final logger logger = loggerfactory.getlogger (current class name. class); you can use the annotation @ Slf4j;
If you want to use it, you have to add the dependency package:

 <dependency>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>1.16.16</version>
    </dependency>

Wait, Zhou Yunpeng said, "do you think it can be used normally? Then you are quite wrong! "
You'll find that it'll also report a mistake. I can't use it. You have to download the "Mom blog lombok" plug-in.

[hope]

Start calling, baby!
As we mentioned earlier, the port number and service number have been configured.
In the address of the loading browser, add the implementation class and the specific method.

OK, call complete

PS: the class just described can be generated through development tools! No explanation

Well, someone else asked: I want to return the data to the front desk!
A: you won't try it on your own. Just change void to value return.
Come to Xu Wei's "empty valley orchid":

@GetMapping("/hoyl")
public String genToken() {
    System.err.println("Time: January 29, 2020 16:58:15,test data");
    for (int i = 0; i < 10; i++) {
        Date date = new Date();
        long times = date.getTime();
        System.err.println("Time test data:"+times);
        log.info("Time test data:"+times);
    }
    return "If you have a beautiful lady, you will have a thousand misfortunes, and your heart will be gone forever, and you will feel sad about the top of the green peak " +
            "The mountain outside the mountain, the sunset is still shining, the starry night is sleepless, like a dream, a glimpse of the world " +
            "The end of the song: sadness and happiness meet each other " +
            "Your supreme cool, quiet and bright in the world of mortals" +
            " Shine on the world in silence " +
            "Who is listening to the sound of empty valley? Pure heart flowers are blooming all over the world" +
            " It's like waking up in a dream to be free in the world with Jingxin" +
            " On the way back to the world, listening to the late wind and the sound of willows and flutes " +
            "Step through the misty rain of the shoes, let your life be wise, firm, brave, and eternal!";
}

Posted by blakey on Wed, 29 Jan 2020 07:39:19 -0800