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包并实现对数据库的操作

相关推荐
大学生资源网1 天前
java毕业设计之面向校园的助力跑腿系统设计与实现源码(源码+文档+数据库)
java·数据库·mysql·毕业设计·源码·springboot
原来是好奇心1 天前
深入Spring Boot源码(五):外部化配置与Profile机制深度解析
java·源码·springboot
摆烂z2 天前
maven中打包不打全部包+多线程打包
java·开发语言·maven
沧澜sincerely2 天前
WebSocket 实时聊天功能
网络·websocket·vue·springboot
计算机毕设指导62 天前
基于微信小程序的校园食堂点餐系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·intellij-idea
素雪风华2 天前
只使用Docker+Maven实现全自动化流程部署服务;Docker创建ffmpeg环境;
java·运维·后端·docker·容器·自动化·maven
计算机学姐2 天前
基于SSM的社区外来务工人员管理系统【2026最新】
java·vue.js·java-ee·tomcat·maven·intellij-idea·mybatis
后端小张2 天前
【JAVA 进阶】深入拆解SpringBoot自动配置:从原理到实战的完整指南
java·开发语言·spring boot·后端·spring·spring cloud·springboot
FLGB2 天前
maven漏洞检测报告
java·maven
本地运行没问题2 天前
依赖找不到?尊嘟假嘟?还不是仓库没配好
maven