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

相关文章
相关标签/搜索