码迷,mamicode.com
首页 > 编程语言 > 详细

学习 - SpringBoot hello

时间:2017-12-28 12:05:21      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:html   control   body   pen   new   ase   manage   enc   nbsp   

1. 下载一个 Intellij IDEA

2. 创建一个新项目,Maven工程

   2.1 pom.xml 引入 它可以提供dependency management,也就是说依赖管理,引入以后在申明其它dependency的时候就不需要version了。

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>

  2.2 开发的是web工程,所以需要在pom.xml中引入spring-boot-starter-web,spring官方解释说spring-boot-start-web包含了spring webmvc和tomcat等web开发的特性。

  pom.xml 引入 

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>

  2.3 如果我们要直接Main启动spring,那么以下plugin必须要添加,否则是无法启动的。如果使用maven的spring-boot:run的话是不需要此配置的。

  pom.xml 引入

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

  2.4 编写一个java类

@RestController
@SpringBootApplication
public class App {

@RequestMapping(value = "/api/hello", method = RequestMethod.GET)
@ResponseBody
public Hello hello() {
Hello hello = new Hello();
hello.setName("Alex");
hello.setSex("M");
return hello;
}

public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

  2.5 其中@SpringBootApplication申明让spring boot自动给程序进行必要的配置,等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan

@RestController返回json字符串的数据,直接可以编写RESTFul的接口;

 

3. 运行,Run Main方法

浏览器中访问 http://127.0.0.1:8080/api/hello

页面显示:

{"name":"Alex","sex":"M"}

QA1:在Mac上启动tomcat时,报了如下错误:

Error: Exception thrown by the agent : java.net.MalformedURLException: Local host name unknown: java.net.UnknownHostException: XXXX: XXXX: nodename nor servname provided, or not known

解决办法:

   打开mac终端,输入:

scutil ––get HostName
scutil ––set HostName "localhost"

QA2:mac hosts 位置

打开Find 后,commond + shift + go

/private/etc 下 hosts

 

 

学习 - SpringBoot hello

标签:html   control   body   pen   new   ase   manage   enc   nbsp   

原文地址:https://www.cnblogs.com/yanghongkuan/p/8133757.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!