Getting Small Demo-web development

Bootstrap class

Just create a bootstrap class.

package cn.itcast.demo;

import org.springframework.boot.SpringApplication;

importorg.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class Application {

    public static void main(String[] args) {

       SpringApplication.run(Application.class, args);

    }

}

Briefly explain, @ SpringBootApplication is actually the sum of the following three notes

@Configuration: class is used to define a configuration

@EnableAutoConfiguration: Spring Boot automatically based on your jar package dependencies automatically configure the project.

@ComponentScan: Spring tells the class which packages are marked with the annotation automatically scan and spring loaded bean container.

We direct the implementation of this guidance class, you will find this logo console appears

 

 

 

You can see the top of this chart is what?

Spring MVC achieve output Hello World

We now start using spring MVC framework, to achieve the output json data. If you follow our original approach, we need to add a DispatcherServlet configured in web.xml, add a spring configuration file, the configuration file to add the following configuration

    <! - component scans used without the controller arranged in the spring ->

           <context:component-scan base-package="cn.itcast.demo.controller" />

    <! - do not use annotations in the lower drive mappings and define appropriate Configurator ->

    <mvc:annotation-driven>

    <mvc:message-converters register-defaults="true">

    <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"

    <property name="supportedMediaTypes" value="application/json"/>

    <property name="features">

    <array>

    <value>WriteMapNullValue</value>

    <value>WriteDateUseDateFormat</value>

    </array>

    </property>

    </bean>

    </mvc:message-converters>

    </mvc:annotation-driven>

But we SpringBoot, it's all saved. We write directly Controller class

package cn.itcast.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class HelloWorldController {

    @RequestMapping("/info")

    public String info(){

       return "HelloWorld";     

    }     

}

We like to run the program to start running

Enter in the browser address bar http: // localhost: 8080 / info you can see the results

Modify tomcat start port

Application.properties created in src / main / resources

server.port=8088

重新运行引导类。地址栏输入http://localhost:8088/info

Guess you like

Origin www.cnblogs.com/coder-wf/p/12518379.html