学习 - SpringBoot hello

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

 

 

转载于:https://www.cnblogs.com/yanghongkuan/p/8133757.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值