Spring Boot(第一章 Controller(接口层)

(1)

@SpringBootApplication//将资源文件读取扫描到容器进行统一管理,在根据容器实现相应的功能
public class DemoApplication {
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

(2)
HTTP访问接口:
1.controller(接口层):servlet属于接口层,接收前端发送的请求,@RestController(接口控制器)
2.service(业务逻辑层):实现业务,例如加减乘除 @service
3.entity(实体层,数据库层) @Entity

@RestController是类的注解,实现@Controller和@ResponseBody两个注解的组合作用,接口类文件
@GetMapping是方法的注解,主要作用是设置方法的访问路径并且限定访问方式是Get

package com.example.demo;

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

@RestController //接口层,是类的注解
@RequestMapping("/first")//模块路径,放在根路径后面
public class FirstController {
    @GetMapping("/hello")//接口路径请求方式,是方法的注解,"/hello"是hello方法的请求路径
    public String hello(){
        return "Hello World";
    }
}

在shixian这里插入图片描述
(3)描述pox.xml的内容,pom是meavn的配置管理数据

 <groupId>com.example</groupId>     项目唯一标识
    <artifactId>demo5</artifactId>  项目名称
    <version>0.0.1-SNAPSHOT</version>   版本
    <name>demo5</name>     项目的名称,Maven产生的文档
    <description>Demo project for Spring Boot</description>  项目的描述

(4)Lombok插件的使用
Lombok被通过注解在编译时自动生成构造器,getter/setter,equals,hashcode,toString等方法,在源代码中没有getter和setter方法,但在但实际上在编译生成的字节码文件中是有这样的方法,省去手动重建代码的麻烦,使代码更加简洁

  1. 创建一个实体类entity `
@Data// @Data注解的主要作用是提高代码的简洁,使用这个注解可以省去实体类中大量的get()、 set()、 toString()等方法。
public class User{
    Integer id;
    String name;
}`
  `@RestController//类的注解,是将当前类作为控制类调价到容器中
public class FirstController {
    @GetMapping("/getuser")
    public User getuser(){
        User user=new User();//实例化一个User对象
        user.setId(1001);
        user.setName("刘欣怡");
        return user;
    }

}`

在这里插入图片描述
(5)全局配置文件的配置
1.改变端口号,文件的根路径在application.properties(项目的运行配置信息)文件中进行修改

server.port=8888//修改端口号
server.servlet.context-path=/springbootclass   //修改根路径

文件的端口号和路径随之改变
在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐