栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

idea搭建maven程序:03-80端口子程序的代码部分(RestTempl)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

idea搭建maven程序:03-80端口子程序的代码部分(RestTempl)

目录

一、说明

1.1 80端口

1.2 RestTemplate

1.3 负载均衡(SOA)

二、80端口子程序的代码部分

2.1 新建

2.2 pom.xml

2.3 application.yml

2.4 程序入口 UsersMain80.class

2.5 entities包和实体类

2.6 config包和对应类

2.7 controller包和对应类

三、运行


一、说明

1.1 80端口

一般用于程序入口,不进行操作,只相当于前台,通过RestTempl跳转到各个子程序

1.2 RestTemplate

是从Spring3.0开始支持的一个HTTP请求工具,它提供了常见的REST请求方案的模版,例如GET请求、POST请求.PUT请求、DELETE请求以及一些通用的请求执行方法exchange 以及execute。

1.3 负载均衡(SOA)

多人访问,达到访问上限,进行等待或分流跨域(RestTemplate)

实现不同工程之间的数据传递

二、80端口子程序的代码部分

总体结构:

2.1 新建

右键总项->new->module->命名cloud-demo-users80

2.2 pom.xml


    
        cc_maven_demo
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    cloud_demo-users80

    
    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.springframework.boot
        spring-boot-starter-actuator
    

    
        org.springframework.boot
        spring-boot-devtools
        runtime
        true
    

    
        org.projectlombok
        lombok
        true
    

    
        org.springframework.boot
        spring-boot-starter-test
        test
    
    

2.3 application.yml
server:
  port: 80

2.4 程序入口 UsersMain80.class
package com.users;

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

//启动项
@SpringBootApplication
public class UsersMain80 {
    public static void main(String[] args) {
        SpringApplication.run(UsersMain80.class,args);
    }
}

2.5 entities包和实体类
package com.users.entities;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data//getset
@AllArgsConstructor//全参构造
@NoArgsConstructor//无参构造
public class TabUsers implements Serializable {//框架要求继承的
    private String id;
    private String loginname;
    private String password;
    private String username;

}

2.6 config包和对应类
package com.users.config;

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

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

2.7 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.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

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

    @Resource//依赖注入,跨域调用
    private RestTemplate rest;

    @GetMapping(value="users/tab_users_findall")//调用
    public String tab_users_findall(){
        //getForObject方法有三个参数,url、传入参数类对象、返回参数类对象
        return rest.getForObject(URL+"/tab_users_findall",String.class, TabUsers.class);//String.class仅占位,传什么参,没传所以占位
    }
}

三、运行

http://localhost/users/tab_users_findall

80是默认端口,所以不用:也行

运行80端口需要跳转时另一个也得运行

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/603010.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号