maven打包项目,然后给其他项目引用

A项目(这个项目需要被打包,作为被引入的项目),不需要启动类,因为作为公共模块被B项目引入:

java 复制代码
package com.yunya.mvndependontest.rest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/teacher")
public class TeacherRest {

    @RequestMapping("/say")
    public String say() {
        String word = "好好学习,天天向上!";
        System.out.println(word);
        return word;
    }

}
java 复制代码
Teaching Service类,包含math 和 chinese方法:

package com.yunya.mvndependontest.service;

import org.springframework.stereotype.Service;

@Service
public class Teaching {

    public String math() {
        return "教数学啦";
    }

    public String chinese() {
        return "教中文啦";
    }

}

B项目:引入打包后的A项目(gav格式引入),启动端口是:8082

java 复制代码
MvnDependOnParentTestApplication 启动类文件:
注意务必要使用@ComponentScan注解将A项目的restcontroller路径也引入,否则访问A项目restful接口路径(/teacher/say)会提示找不到地址。
package com.yunya.mvndependonparenttest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.yunya")
public class MvnDependOnParentTestApplication {

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

}
java 复制代码
Student RestController类,该类中调用A项目的Teaching Service类的方法math:

package com.yunya.mvndependonparenttest.rest;

import com.yunya.mvndependontest.service.Teaching;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/student")
public class Student {

    @Autowired
    Teaching teaching;

    @RequestMapping("/study")
    public String study() {
        String studyContent = "学生学到了一首诗:床前明月光,疑是地上霜。";
        System.out.println(studyContent);

        // 调用引入的A项目的teaching service类中的math方法
        String mathContent = teaching.math();
        System.out.println(mathContent);

        return studyContent;
    }
}

访问效果:

访问B项目地址:localhost:8082/teacher/say,如下效果:

访问B项目地址:localhost:8082/student/study,如下效果:

引用:
把一个项目打成jar包并引入其他项目中

springboot引入其他项目jar包并实现对数据库的操作

相关推荐
小宇宙Zz4 天前
Maven依赖冲突
java·服务器·maven
十五喵源码网4 天前
基于springboot2+vue2的租房管理系统
java·毕业设计·springboot·论文笔记
砚底藏山河4 天前
沪深A股:如何获取基金持股数据
java·python·数据分析·maven
一勺菠萝丶4 天前
Maven SNAPSHOT 父 POM 无法解析问题排查
java·maven
我登哥MVP4 天前
SpringCloud Alibaba 核心组件解析:服务链路追踪
java·spring boot·后端·spring·spring cloud·java-ee·maven
南部余额4 天前
Maven Archetype 项目模板
java·maven·项目·archetype
梦想的旅途24 天前
企业微信外部群自动化:一期交付应聚焦双向会话闭环
java·开发语言·机器人·自动化·maven·企业微信
vx-Biye_Design4 天前
springboot安阳地区研学旅游服务小程序-计算机毕业设计源码12785
java·vue.js·windows·spring boot·tomcat·maven·mybatis
whaledown4 天前
Kafka 与 Java 消息队列入门:用订单场景理解核心机制
java·kafka·消息队列·springboot
Clang's Blog4 天前
Ubuntu(20.04/22.04/24.04)国内环境一键安装 Docker、JDK17 和 Maven
ubuntu·docker·maven