构建简单的项目

通常微服务都是由一组项目构建而成的一套服务架构,所以为了确保版本的一致性,通常都会用一个父工程来管理jar依赖版本,首先我们创建一个pom作为父工程

构建工程

在父工程下引入springboot依赖包

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

在子工程中添加依赖

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

之后和编写springmvc是差不多的,但是springboot作为微服务只提供数据和方法,并不关心页面的跳转,所以此处不再使用controller注解,而是使用RestController注解,该注解实际上是复合注解,相当于Controller注解 + ResponseBody注解以流形式输出,而不是交给视图解析器去转发页面.
接下来的编写和springmvc一样,方法上加上@RequestMapping注解,我们简单编写一个.

		@RestController
		public class TestController{
		@RequestMappin("/test")
		public String index(){
			return "abcd";	
		}
	}

接下来我们再编写一个启动类,需要注意的是 启动类只能在controller的同包或者父包下,绝对不能放在其他包中,并且该类需要由一个注解来说明是springboot的启动类 @SpringBootApplication
在main方法中启动我们的项目 SpringApplication.run

SpringBoot教程(一)创建一个SpringBoot项目

由上图我们可以看出该项目启动的默认端口号为8080
我们看访问一下看看

SpringBoot教程(一)创建一个SpringBoot项目

相关文章:

  • 2022-01-31
  • 2022-12-23
  • 2021-12-04
  • 2021-05-24
  • 2021-12-04
  • 2021-06-21
猜你喜欢
  • 2021-08-21
  • 2021-12-13
  • 2021-11-17
  • 2021-06-06
  • 2021-11-29
  • 2021-08-14
  • 2021-08-06
相关资源
相似解决方案