SpringBoot入门程序HelloWorld
1.创建一个maven项目spring-boot-hello
2.编写pom.xml
<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.pypua</groupId> <artifactId>spring-boot-hello</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-hello</name> <url>http://maven.apache.org</url> <!-- spring boot 父节点依赖 ,引入这个之后相关的引入就不需要添加version配置,spring boot会自动选择最合适的版本添加--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 --> <java.version>1.8</java.version> </properties> <dependencies> <!-- spring-boot-starter-web: MVC,AOP的依赖包....--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- <version></version> 由于我们在上面指定了 parent(spring boot) --> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.6.1</version> </dependency> <!-- 添加fastjson 依赖包. --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version> </dependency> </dependencies> </project>
3.编写Controller类
package com.pypua; import java.util.Date; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController /** * 这里使用@RestController相当于@Controller和@RequestBody * @author pypua * */ public class helloController { @RequestMapping("/hello") public String hello(){ return "hello"; } /** * spring boot默认使用的json框架是jackson * @return */ @RequestMapping("/getDemo") public demo getDemo(){ demo demo = new demo(); demo.setId(1); demo.setName("张三"); demo.setCreateTime(new Date()); demo.setRemarks("这个是测试"); return demo; } }
4.编写App.java
package com.pypua; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import javax.security.auth.message.callback.PrivateKeyCallback.Request; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.web.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; /** * Hello world! *在这里我们使用@SpringBootApplication指定这是一个spring boot应用程序 */ @SpringBootApplication public class App { public static void main( String[] args ) { //在main方法中启动应用程序 SpringApplication.run(App.class,args); } }
划船不用桨、杨帆不等风、一生全靠浪