iT邦幫忙

2025 iThome 鐵人賽

DAY 2
0
AI & Data

ML/DL實作-「營養抗老」專題製作系列 第 2

Day2_ML/DL實作-「營養抗老」專題-環境「mySpringbootmall」建置

  • 分享至 

  • xImage
  •  
  • 今天我們會把mySpringbootmall的空殼專案建立起來,本系列是主題是AI&Data,所以關於Spring Boot的知識不會詳加說明,這部分請讀者自行上網學習相關基本知識。

  • 首先,會建立一個mySpringbootmall的專案,自行建立測試用的假資料,用它來測試mySpringbootmall是否可正常執行。

  • 工作目錄 ~/mywork/

  • 專案架構如圖:

  • https://ithelp.ithome.com.tw/upload/images/20250903/20168324bAJD947LnG.png

  • pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.3.3</version>
    <relativePath/>
  </parent>

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.grace</groupId>
  <artifactId>mySpringbootmall</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>mySpringbootmall</name>
  <description>ML/DL 實作-營養抗老專案</description>

  <properties>
    <java.version>21</java.version>
    <spring.boot.version>3.3.3</spring.boot.version>
  </properties>

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

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
         <executions>
          <execution>
           <goals>
             <goal>repackage</goal>
           </goals>
         </execution>
       </executions>
      </plugin>
    </plugins>
  </build>
</project>

  • EffectController.java
package com.grace.mySpringbootmall.controller;

import java.util.Arrays;
import java.util.List;

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

import com.grace.mySpringbootmall.dto.EffectDto;

@RestController
@RequestMapping("/api")
public class EffectController {

    @GetMapping("/effects")
    public List<EffectDto> listEffects() {
        return Arrays.asList(
            new EffectDto("E001", "烏髮(減少白髮/促黑色素)", "皮膚毛髮"),
            new EffectDto("E002", "抗皺(細紋/彈性)", "皮膚抗老")
        );
    }
}
  • RecommendationController.java
package com.grace.mySpringbootmall.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.grace.mySpringbootmall.dto.RecommendationResponse;
import com.grace.mySpringbootmall.service.RecommendationService;

import jakarta.validation.constraints.Pattern;

@RestController
@RequestMapping("/api")
@Validated
public class RecommendationController {

    @Autowired
    private RecommendationService recommendationService;

    @GetMapping("/recommendations")
    public RecommendationResponse recommend(
        @RequestParam
        String effectId) {
        return recommendationService.recommend(effectId);
    }
}

  • EffectDto.java
package com.grace.mySpringbootmall.dto;

public class EffectDto {
    private String effectId;    // E001 / E002
    private String effectName;  // 烏髮 / 抗皺
    private String category;    // 皮膚毛髮 / 皮膚抗老

    public EffectDto() {}

    public EffectDto(String effectId, String effectName, String category) {
        this.effectId = effectId;
        this.effectName = effectName;
        this.category = category;
    }

    public String getEffectId() { return effectId; }
    public void setEffectId(String effectId) { this.effectId = effectId; }

    public String getEffectName() { return effectName; }
    public void setEffectName(String effectName) { this.effectName = effectName; }

    public String getCategory() { return category; }
    public void setCategory(String category) { this.category = category; }
}

  • MockScoringStrategy.java
package com.grace.mySpringbootmall.service.scoring;

import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Component;
import com.grace.mySpringbootmall.dto.RecommendationItem;

@Component
public class MockScoringStrategy implements ScoringStrategy {

    @Override
    public List<RecommendationItem> score(String effectId) {
        if ("E001".equalsIgnoreCase(effectId)) { // 烏髮
            return Arrays.asList(
                new RecommendationItem("food", "黑芝麻", 7.2, "C", Arrays.asList("PMID:demo")),
                new RecommendationItem("food", "綠茶", 6.5, "C", Arrays.asList("PMID:demo")),
                new RecommendationItem("food", "雞蛋(維生素B12來源)", 6.0, "C", Arrays.asList("PMID:demo"))
            );
        } else if ("E002".equalsIgnoreCase(effectId)) { // 抗皺
            return Arrays.asList(
                new RecommendationItem("food", "番茄", 8.0, "B", Arrays.asList("PMID:demo")),
                new RecommendationItem("food", "甜椒(維生素C)", 7.5, "C", Arrays.asList("PMID:demo")),
                new RecommendationItem("food", "鮭魚", 7.0, "C", Arrays.asList("PMID:demo"))
            );
        }
        return Arrays.asList();
    }
}


  • ScoringStrategy.java
package com.grace.mySpringbootmall.service.scoring;

import java.util.List;
import com.grace.mySpringbootmall.dto.RecommendationItem;

public interface ScoringStrategy {
    List<RecommendationItem> score(String effectId);
}

  • RecommendationService.java
package com.grace.mySpringbootmall.service;

import com.grace.mySpringbootmall.dto.RecommendationResponse;

public interface RecommendationService {
    RecommendationResponse recommend(String effectId);
}

  • RecommendationServiceImpl.java
package com.grace.mySpringbootmall.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.grace.mySpringbootmall.dto.RecommendationItem;
import com.grace.mySpringbootmall.dto.RecommendationResponse;
import com.grace.mySpringbootmall.service.scoring.ScoringStrategy;

@Service
public class RecommendationServiceImpl implements RecommendationService {

    @Autowired
    private ScoringStrategy scoring;

    @Override
    public RecommendationResponse recommend(String effectId) {
        List<RecommendationItem> items = scoring.score(effectId);
        return new RecommendationResponse(effectId, items);
    }
}

  • applicaiton.properties
server.port=8080
spring.main.banner-mode=off
logging.level.org.springframework.web=INFO

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

測試方法,在terminal(~/mywork/mySpringbootmall):

mvn clean package -skipTests
java -jar target/mySpringbootmall-0.0.1-SNAPSHOT.jar 
 
  • 昨日,是安裝建立專案的必要軟體,今天建立mySpringbootmall和mock data,明日,我們要建立DB Schema.

上一篇
Day1_ML/DL實作-「營養抗老」專題-環境建置
下一篇
Day3_ML/DL實作-「營養抗老」專題- db schema 建立
系列文
ML/DL實作-「營養抗老」專題製作5
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言