SpringBoot(第一章Controller(接口层)
@SpringBootApplication//将资源文件读取扫描到容器进行统一管理,在根据容器实现相应的功能
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
HTTP访问接口:controller(接口层):servlet属于接口层,接收前端发送的请求,@RestControllerservice(业务逻辑层):实现业务,例如加减乘除@serviceentity(实体层,数据库层)@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';
}
}
描述pox.xml的内容,pom是meavn的配置管理数据
com.example 项目唯一标识
demo5 项目名称
0.0.1-SNAPSHOT 版本
demo5 项目的名称,Maven产生的文档
Demo project for Spring Boot 项目的描述
Lombok插件的使用Lombok被通过注解在编译时自动生成构造器,getter/setter,equals,hashcode,toString等方法,在源代码中没有getter和setter方法,但在但实际上在编译生成的字节码文件中是有这样的方法,省去手动重建代码的麻烦,使代码更加简洁
创建一个实体类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;
}
}`
全局配置文件的配置改变端口号,文件的根路径在applicatioproperties文件中进行修改
文章为作者独立观点,不代表 股票程序化软件自动交易接口观点