第一步:创建maven工程,此过程不再赘述
第二部:在pom中添加依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.huawei</groupId> <artifactId>spring-boot-hello</artifactId> <version>1.0-SNAPSHOT</version> <name>spring-boot-hello</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency><!--MVC,AOP的依赖包--> <!--在parent中已经设置了version,这里就不需要设置,spring会自动设置合适的version--> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <build> <pluginManagement> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> </plugins> </pluginManagement> </build> </project>
第三步:创建HelloController类
package com.huawei;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* RestController等价于@Controller和@RequestBody
*/
@RestController
public class HelloController
{
/**
* 这里我们使用RequestMapping 建立请求映射
* @return
*/
@RequestMapping("/hello")
public String hello() {
return "hello";
}
/**
* springBoot默认使用的json解析框架是jackson
* @return
*/
@RequestMapping("demo")
public Demo getDemo() {
Demo demo = new Demo();
demo.setId(1);
demo.setName("张三");
return demo;
}
}
第四步:配置入口类:
package com.huawei;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.util.ArrayList;
import java.util.List;
/**
* 使用SpringBootApplication指定一个spring boot的启动类
* Hello world!
*
*/
@SpringBootApplication
public class App extends WebMvcConfigurationSupport
{
/**
* 在main方法中启动我们的应用程序
* @param args
*/
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
第五步:运行

在浏览器输入:http://127.0.0.1:8080/hello

1235

被折叠的 条评论
为什么被折叠?



