微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpringCloud——RestTempl

框图

在这里插入图片描述

负载均衡(SOA)

多人访问,达到访问上限,进行等待或分流

跨域(RestTemplate)

不同工程之间的数据传递

完善SpringCloud项目

创建“前台

1.创建module,命名cloud-demo-users80

2.书写pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>SpringCloudLearn</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-demo-users80</artifactId>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

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

</project>

3.创建application.yml

server:
  port: 80

4.创建包与启动类

在这里插入图片描述

写config文件

package com.users.config;

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

@Configuration
public class ApplicationContextConfig {
    //能够提供多种便捷访问远程HTTP服务的方法
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

写Controller

package com.users.controller;

import com.users.entities.TabUsers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

import static com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundOperation.ANONYMOUS.@R_404_3889@;

@RestController
@Slf4j
public class UserController {
    public static final String URL="http://localhost:8001";

    @Resource
    private RestTemplate rest;

    @GetMapping("users/tab_users_findall")
    public String tab_users_findall(){
        return rest.getForObject(URL+"/tab_users_findall",String.class, TabUsers.class);
    }

    @PostMapping("users/tab_users_save")
    public String save(@RequestBody TabUsers t){
        return rest.getForObject(URL+"/tab_users_save",String.class, TabUsers.class);
    }
}

写启动类

package com.users;


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

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

测试

在这里插入图片描述

写insert方法

class_demo8001

mapper

<insert id="save" parameterType="TabUsers">
    insert into tab_users values(#{id},#{loginname},#{password},#{username})
</insert>

Controller

@PostMapping(value = "/tab_users_save")
    public String save(@RequestBody TabUsers t){
        String id="4";
        t.setId(id);
        JSONObject jo=new JSONObject();
        int i=service.save(t);
        if(i>0){
            jo.put("code","200");
            jo.put("msg","success");
        }else{
            jo.put("code","500");
            jo.put("msg","error");
        }
        return jo.toJSONString();
    }

注意这里面要有@RequestBody,且value里面要有’/’,否则数据传不进去

cloud-demo-users80

Controller

@GetMapping("users/tab_users_save")
public String save(TabUsers t){
    Map<String,Object> res=rest.postForObject(URL+"/tab_users_save",t, Map.class);
    return res.toString();
}

测试

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐