Spring-Boot2.0搭建controller路由層(二)

1.添加依賴

在pom.xml 文件中添加一層路由的依賴java

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
	</dependency>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>

	<!--web RESTFUL 路由層-->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
</dependencies>

2. 建立路由層的類

添加package包並建立類CoreController.javaweb

添加註解@RestController告訴容器這是路由層spring

添加方法並註解說明請求的地址是/helloapp

package com.demo.controller;

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

@RestController
public class CoreController {
    @RequestMapping("/hello")
    public String hello() {
        return "-hello world-";
    }
}

3.啓動 

啓動DemoApplication的mian方法spring-boot

端口是8080spa

這個時候訪問:http://localhost:8080/hellocode

成功返回「hello world」xml

相關文章
相關標籤/搜索