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

相关推荐
l***217830 分钟前
SpringBoot Maven快速上手
spring boot·后端·maven
小当家.10510 小时前
Maven与Gradle完整对比指南:构建工具深度解析
java·gradle·maven
indexsunny11 小时前
互联网大厂Java面试实战:Spring Boot与微服务在电商场景的应用解析
java·spring boot·redis·微服务·kafka·gradle·maven
qq_54702617911 小时前
Maven 仓库管理
java·maven
shejizuopin13 小时前
基于JavaSSM+MySQL的实验室考勤管理系统设计与实现
java·mysql·vue·毕业设计·论文·springboot·实验室考勤管理系统设计与实现
xiaobaishuoAI14 小时前
后端工程化实战指南:从规范到自动化,打造高效协作体系
java·大数据·运维·人工智能·maven·devops·geo
qq_5470261791 天前
Maven 使用指南
java·maven
计算机毕设指导61 天前
基于微信小程序的钓鱼论坛系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
千寻技术帮2 天前
10349_基于Springboot的万仙山旅游管理系统
mysql·springboot·旅游管理·在线旅游
jasnet_u2 天前
SpringCloudAlibaba的web微服务快速搭建
java·springboot·springlcoud