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

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   //修改根路径

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

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值