Rest环境搭建

Rest环境搭建

父工程:pom文件,导入依赖



4.0.0

org.example
springcloud
1.0-SNAPSHOT

springcloud-api
springcloud-provider-dept-8001
springcloud-consumer-dept-80

pom


<junit.version>4.13.1</junit.version>
<mysql.version>8.0.26</mysql.version>
<druid.version>1.2.4</druid.version>
<lombok.version>1.18.20</lombok.version>
<logback-core.version>1.2.5</logback-core.version>
<mybatis-spring-boot-starter.version>2.1.4</mybatis-spring-boot-starter.version>
<spring-boot-dependencies.version>2.5.4</spring-boot-dependencies.version>
<spring-cloud-dependencies.version>Hoxton.SR9</spring-cloud-dependencies.version>

>




org.springframework.cloud
spring-cloud-dependencies
${spring-cloud-dependencies.version}
pom
import


org.springframework.boot
spring-boot-dependencies
${spring-boot-dependencies.version}
pom
import


mysql
mysql-connector-java
${mysql.version}


com.alibaba
druid
${druid.version}


org.mybatis.spring.boot
mybatis-spring-boot-starter
${mybatis-spring-boot-starter.version}


junit
junit
${junit.version}
test


ch.qos.logback
logback-core
${logback-core.version}


org.projectlombok
lombok
${lombok.version}


数据库表

deptno dname db_source
1 开发部 db01
2 人事部 db01
3 财务部 db01
4 市场部 db01
5 运维部 db01

生产者模块:

provider-dept-8001

1.编写配置文件application.yaml,在pom文件导入需要的包

server:
port: 8001

mybatis配置

mybatis:
type-aliases-package: com.yhc.pojo
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml

spring 配置

spring:
application:
name: springcloud-provider-dept

datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8
username: root
password: 123456

2.编写dao层:

DeptDao

import com.yhc.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface DeptDao {
public boolean addDept(Dept dept);

public Dept queryById(Long id);

public List queryAll();
}

3.编写service层

DeptService

import com.yhc.pojo.Dept;

import java.util.List;

public interface DeptService {

public boolean addDept(Dept dept);

public Dept queryById(Long id);

public List queryAll();
}

DeptServiceImpl

import com.yhc.dao.DeptDao;
import com.yhc.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;

import java.util.List;

@Service
public class DeptServiceImpl implements DeptService{

@Autowired
DeptDao deptDao;

public boolean addDept(Dept dept) {
return deptDao.addDept(dept);
}

public Dept queryById(Long id) {
return deptDao.queryById(id);
}

public List queryAll() {
return deptDao.queryAll();
}
}

4.编写mapper.xml



insert into dept(dname,db_source)
values(#{dname},DATABASE())

5.编写controller层

import com.yhc.pojo.Dept;
import com.yhc.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DeptController {

@Autowired
private DeptService deptService;

@PostMapping("/dept/add")
public boolean addDept(Dept dept){
return deptService.addDept(dept);
}

@GetMapping("/dept/get/{id}")
public Dept get(@PathVariable("id")Long id ){
return deptService.queryById(id);
}

@GetMapping("/dept/list")
public List qurtyAll(){
return deptService.queryAll();
}
}

6.编写主程序类

DeptProvider_8001

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DeptProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_8001.class,args);
}
}

消费者模块

consumer_dept_80

1.在application.yml中配置端口80

server:
port: 80

2.利用rsetTemplate提供便捷访问远程http服务的方法,简单的restful模块

  • 编写配置文件,将rsetTemplate注入bean

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.client.RestTemplate;

    @Configuration
    public class ConfigBean {
    @Bean
    public RestTemplate getRestTemplate(){
    return new RestTemplate();
    }
    }

3.Controller层使用RestTemplate

import com.yhc.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class DeptConsumerController {

@Autowired
private RestTemplate restTemplate;//提供便捷访问远程http服务的方法,简单的restful服务模板

private static final String REST_URL_PREFIX = "http://localhost:8001";

@PostMapping("/consumer/dept/add")
public boolean add(Dept dept) {
return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
}

@RequestMapping("/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") Long id) {
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
}

@RequestMapping("/consumer/dept/list")
public List list() {
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
}
}

4.编写主程序

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DeptConsumer_80 {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer_80.class,args);
}
}

测试运行

posted @ 2021-09-07 17:03  青歌与  阅读(86)  评论(1)    收藏  举报