GitHub Actions commited on
Commit
d7f1ba4
·
0 Parent(s):

deploy: deploy application container to Hugging Face

Browse files
Dockerfile ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # --- Stage 1: Build the Application ---
2
+ FROM eclipse-temurin:21-jdk-alpine AS build
3
+ RUN apk add --no-cache git
4
+ WORKDIR /app
5
+
6
+ # Copy the gradle configuration files first to cache dependencies
7
+ COPY gradlew .
8
+ COPY gradle gradle
9
+ COPY build.gradle .
10
+ COPY settings.gradle .
11
+
12
+ # Dynamically clone the public omni-context submodule repo in the build container
13
+ RUN git clone https://github.com/amritnray/omni-context.git omni-context && \
14
+ cd omni-context && \
15
+ git checkout develop
16
+
17
+ # Ensure gradle execution permissions and download dependencies
18
+ RUN chmod +x gradlew
19
+
20
+ # Note: If 'com.example:spring-boot-mcp-starter:1.0.0' is only available in your local Maven repository,
21
+ # the cloud build will fail to resolve it. If this happens, see Option B in the instructions.
22
+ RUN ./gradlew dependencies --no-daemon || true
23
+
24
+ # Copy source code and build
25
+ COPY src src
26
+ RUN ./gradlew build --no-daemon -x test
27
+
28
+ # --- Stage 2: Create the Lightweight Runner ---
29
+ FROM eclipse-temurin:21-jre-alpine
30
+ EXPOSE 8080
31
+ COPY --from=build /app/build/libs/*-SNAPSHOT.jar app.jar
32
+
33
+ # JVM Tuning: Crucial for free tiers to prevent memory limits from killing the app
34
+ ENTRYPOINT ["java", "-XX:MaxRAMPercentage=75.0", "-XX:+UseG1GC", "-jar", "/app.jar"]
README.md ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: SimpleJavaApp
3
+ emoji: 🚀
4
+ colorFrom: blue
5
+ colorTo: green
6
+ sdk: docker
7
+ app_port: 8080
8
+ pinned: false
9
+ ---
10
+
11
+ # SimpleJavaApp 🚀
12
+
13
+ A modern and production-ready Spring Boot application containerized for easy, free cloud deployment (Render, Koyeb, OCI) .
14
+
15
+ This project features a fully configured **Student Management Service** and comes out of the box with Docker multi-stage build optimization and automated JVM memory tuning specifically designed to prevent Out of Memory (OOM) errors on restricted free-tier hosting platforms.
16
+
17
+ ---
18
+
19
+ ## 🌟 Key Features
20
+
21
+ * **Spring Boot Core**: Web, JPA, H2 In-Memory database, and custom library starter support.
22
+ * **REST APIs**: Full CRUD capabilities for student resource management.
23
+ * **Self-Contained Dependency Management**: Houses the custom `spring-boot-mcp-starter` library locally in `/libs` so that the cloud build compiles flawlessly without external repositories.
24
+ * **Cloud-Optimized Dockerfile**: Uses a multi-stage `eclipse-temurin` Alpine Docker image to produce a ultra-lightweight running container.
25
+ * **Smart JVM Memory Constraints**: Automatically restricts JVM heap space to `75%` of container memory to survive restricted hosting tiers.
26
+
27
+ ---
28
+
29
+ ## 🛠️ How to Run Locally
30
+
31
+ ### Prerequisites
32
+ * **Java SDK**: Version 21
33
+ * **Gradle**: Handled automatically via `./gradlew`
34
+
35
+ ### Run from Terminal
36
+ Clone the repository and run:
37
+ ```bash
38
+ # Run the application
39
+ ./gradlew bootRun
40
+ ```
41
+ The application will start on `http://localhost:8080`.
42
+
43
+ ---
44
+
45
+ ## ☁️ How to Deploy Free on the Cloud (Render)
46
+
47
+ This application is ready to deploy to **Render** with a single click.
48
+
49
+ 1. **Push your code to GitHub**: Create a repository and push this project directory.
50
+ 2. **Log into Render**: Connect using your GitHub account at [Render Dashboard](https://dashboard.render.com).
51
+ 3. **Create a New Web Service**:
52
+ * Click **New +** > **Web Service**.
53
+ * Select your `SimpleJavaApp` repository.
54
+ 4. **Configure Settings**:
55
+ * **Runtime**: `Docker`
56
+ * **Instance Type**: `Free`
57
+ 5. **Launch**: Click **Create Web Service**.
58
+
59
+ Render will automatically read the `Dockerfile`, package the local libraries, compile your source code, and serve it live!
60
+
61
+ ---
62
+
63
+ ## 📁 Project Structure
64
+
65
+ ```text
66
+ ├── libs/ # Local jar dependencies (spring-boot-mcp-starter)
67
+ ├── src/ # Application source code
68
+ │ └── main/java/com/... # Spring controllers, services, repositories
69
+ ├── Dockerfile # High-efficiency multi-stage Docker build
70
+ ├── build.gradle # Build and dependency configuration
71
+ └── README.md # Project documentation
72
+ ```
build.gradle ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ plugins {
2
+ id 'java'
3
+ id 'org.springframework.boot' version '3.2.5'
4
+ id 'io.spring.dependency-management' version '1.1.5'
5
+ }
6
+
7
+ group = 'com.example'
8
+ version = '0.0.1-SNAPSHOT'
9
+
10
+ java {
11
+ toolchain {
12
+ languageVersion = JavaLanguageVersion.of(21)
13
+ }
14
+ }
15
+
16
+ repositories {
17
+ mavenLocal()
18
+ mavenCentral()
19
+ }
20
+
21
+ dependencies {
22
+ implementation 'org.springframework.boot:spring-boot-starter-web'
23
+ implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
24
+ implementation project(':omni-context')
25
+ runtimeOnly 'com.h2database:h2'
26
+ testImplementation 'org.springframework.boot:spring-boot-starter-test'
27
+ }
28
+
29
+ tasks.named('test') {
30
+ useJUnitPlatform()
31
+ }
gradle/wrapper/gradle-wrapper.jar ADDED
Binary file (43.5 kB). View file
 
gradle/wrapper/gradle-wrapper.properties ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ distributionBase=GRADLE_USER_HOME
2
+ distributionPath=wrapper/dists
3
+ distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
4
+ networkTimeout=10000
5
+ validateDistributionUrl=true
6
+ zipStoreBase=GRADLE_USER_HOME
7
+ zipStorePath=wrapper/dists
gradlew ADDED
@@ -0,0 +1,249 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/sh
2
+
3
+ #
4
+ # Copyright © 2015-2021 the original authors.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # https://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ ##############################################################################
20
+ #
21
+ # Gradle start up script for POSIX generated by Gradle.
22
+ #
23
+ # Important for running:
24
+ #
25
+ # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
26
+ # noncompliant, but you have some other compliant shell such as ksh or
27
+ # bash, then to run this script, type that shell name before the whole
28
+ # command line, like:
29
+ #
30
+ # ksh Gradle
31
+ #
32
+ # Busybox and similar reduced shells will NOT work, because this script
33
+ # requires all of these POSIX shell features:
34
+ # * functions;
35
+ # * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
36
+ # «${var#prefix}», «${var%suffix}», and «$( cmd )»;
37
+ # * compound commands having a testable exit status, especially «case»;
38
+ # * various built-in commands including «command», «set», and «ulimit».
39
+ #
40
+ # Important for patching:
41
+ #
42
+ # (2) This script targets any POSIX shell, so it avoids extensions provided
43
+ # by Bash, Ksh, etc; in particular arrays are avoided.
44
+ #
45
+ # The "traditional" practice of packing multiple parameters into a
46
+ # space-separated string is a well documented source of bugs and security
47
+ # problems, so this is (mostly) avoided, by progressively accumulating
48
+ # options in "$@", and eventually passing that to Java.
49
+ #
50
+ # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
51
+ # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
52
+ # see the in-line comments for details.
53
+ #
54
+ # There are tweaks for specific operating systems such as AIX, CygWin,
55
+ # Darwin, MinGW, and NonStop.
56
+ #
57
+ # (3) This script is generated from the Groovy template
58
+ # https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
59
+ # within the Gradle project.
60
+ #
61
+ # You can find Gradle at https://github.com/gradle/gradle/.
62
+ #
63
+ ##############################################################################
64
+
65
+ # Attempt to set APP_HOME
66
+
67
+ # Resolve links: $0 may be a link
68
+ app_path=$0
69
+
70
+ # Need this for daisy-chained symlinks.
71
+ while
72
+ APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
73
+ [ -h "$app_path" ]
74
+ do
75
+ ls=$( ls -ld "$app_path" )
76
+ link=${ls#*' -> '}
77
+ case $link in #(
78
+ /*) app_path=$link ;; #(
79
+ *) app_path=$APP_HOME$link ;;
80
+ esac
81
+ done
82
+
83
+ # This is normally unused
84
+ # shellcheck disable=SC2034
85
+ APP_BASE_NAME=${0##*/}
86
+ # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
87
+ APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
88
+
89
+ # Use the maximum available, or set MAX_FD != -1 to use that value.
90
+ MAX_FD=maximum
91
+
92
+ warn () {
93
+ echo "$*"
94
+ } >&2
95
+
96
+ die () {
97
+ echo
98
+ echo "$*"
99
+ echo
100
+ exit 1
101
+ } >&2
102
+
103
+ # OS specific support (must be 'true' or 'false').
104
+ cygwin=false
105
+ msys=false
106
+ darwin=false
107
+ nonstop=false
108
+ case "$( uname )" in #(
109
+ CYGWIN* ) cygwin=true ;; #(
110
+ Darwin* ) darwin=true ;; #(
111
+ MSYS* | MINGW* ) msys=true ;; #(
112
+ NONSTOP* ) nonstop=true ;;
113
+ esac
114
+
115
+ CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
116
+
117
+
118
+ # Determine the Java command to use to start the JVM.
119
+ if [ -n "$JAVA_HOME" ] ; then
120
+ if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
121
+ # IBM's JDK on AIX uses strange locations for the executables
122
+ JAVACMD=$JAVA_HOME/jre/sh/java
123
+ else
124
+ JAVACMD=$JAVA_HOME/bin/java
125
+ fi
126
+ if [ ! -x "$JAVACMD" ] ; then
127
+ die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
128
+
129
+ Please set the JAVA_HOME variable in your environment to match the
130
+ location of your Java installation."
131
+ fi
132
+ else
133
+ JAVACMD=java
134
+ if ! command -v java >/dev/null 2>&1
135
+ then
136
+ die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
137
+
138
+ Please set the JAVA_HOME variable in your environment to match the
139
+ location of your Java installation."
140
+ fi
141
+ fi
142
+
143
+ # Increase the maximum file descriptors if we can.
144
+ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
145
+ case $MAX_FD in #(
146
+ max*)
147
+ # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
148
+ # shellcheck disable=SC2039,SC3045
149
+ MAX_FD=$( ulimit -H -n ) ||
150
+ warn "Could not query maximum file descriptor limit"
151
+ esac
152
+ case $MAX_FD in #(
153
+ '' | soft) :;; #(
154
+ *)
155
+ # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
156
+ # shellcheck disable=SC2039,SC3045
157
+ ulimit -n "$MAX_FD" ||
158
+ warn "Could not set maximum file descriptor limit to $MAX_FD"
159
+ esac
160
+ fi
161
+
162
+ # Collect all arguments for the java command, stacking in reverse order:
163
+ # * args from the command line
164
+ # * the main class name
165
+ # * -classpath
166
+ # * -D...appname settings
167
+ # * --module-path (only if needed)
168
+ # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
169
+
170
+ # For Cygwin or MSYS, switch paths to Windows format before running java
171
+ if "$cygwin" || "$msys" ; then
172
+ APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
173
+ CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
174
+
175
+ JAVACMD=$( cygpath --unix "$JAVACMD" )
176
+
177
+ # Now convert the arguments - kludge to limit ourselves to /bin/sh
178
+ for arg do
179
+ if
180
+ case $arg in #(
181
+ -*) false ;; # don't mess with options #(
182
+ /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
183
+ [ -e "$t" ] ;; #(
184
+ *) false ;;
185
+ esac
186
+ then
187
+ arg=$( cygpath --path --ignore --mixed "$arg" )
188
+ fi
189
+ # Roll the args list around exactly as many times as the number of
190
+ # args, so each arg winds up back in the position where it started, but
191
+ # possibly modified.
192
+ #
193
+ # NB: a `for` loop captures its iteration list before it begins, so
194
+ # changing the positional parameters here affects neither the number of
195
+ # iterations, nor the values presented in `arg`.
196
+ shift # remove old arg
197
+ set -- "$@" "$arg" # push replacement arg
198
+ done
199
+ fi
200
+
201
+
202
+ # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
203
+ DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
204
+
205
+ # Collect all arguments for the java command:
206
+ # * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
207
+ # and any embedded shellness will be escaped.
208
+ # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
209
+ # treated as '${Hostname}' itself on the command line.
210
+
211
+ set -- \
212
+ "-Dorg.gradle.appname=$APP_BASE_NAME" \
213
+ -classpath "$CLASSPATH" \
214
+ org.gradle.wrapper.GradleWrapperMain \
215
+ "$@"
216
+
217
+ # Stop when "xargs" is not available.
218
+ if ! command -v xargs >/dev/null 2>&1
219
+ then
220
+ die "xargs is not available"
221
+ fi
222
+
223
+ # Use "xargs" to parse quoted args.
224
+ #
225
+ # With -n1 it outputs one arg per line, with the quotes and backslashes removed.
226
+ #
227
+ # In Bash we could simply go:
228
+ #
229
+ # readarray ARGS < <( xargs -n1 <<<"$var" ) &&
230
+ # set -- "${ARGS[@]}" "$@"
231
+ #
232
+ # but POSIX shell has neither arrays nor command substitution, so instead we
233
+ # post-process each arg (as a line of input to sed) to backslash-escape any
234
+ # character that might be a shell metacharacter, then use eval to reverse
235
+ # that process (while maintaining the separation between arguments), and wrap
236
+ # the whole thing up as a single "set" statement.
237
+ #
238
+ # This will of course break if any of these variables contains a newline or
239
+ # an unmatched quote.
240
+ #
241
+
242
+ eval "set -- $(
243
+ printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
244
+ xargs -n1 |
245
+ sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
246
+ tr '\n' ' '
247
+ )" '"$@"'
248
+
249
+ exec "$JAVACMD" "$@"
settings.gradle ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ plugins {
2
+ id 'org.gradle.toolchains.foojay-resolver-convention' version '0.8.0'
3
+ }
4
+
5
+ rootProject.name = 'SimpleJavaApp'
6
+ include 'omni-context'
src/main/java/com/example/SimpleJavaApp/SimpleJavaAppApplication.java ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.SimpleJavaApp;
2
+
3
+ import org.springframework.boot.SpringApplication;
4
+ import org.springframework.boot.autoconfigure.SpringBootApplication;
5
+
6
+ @SpringBootApplication
7
+ public class SimpleJavaAppApplication {
8
+
9
+ public static void main(String[] args) {
10
+ SpringApplication.run(SimpleJavaAppApplication.class, args);
11
+ }
12
+ }
src/main/java/com/example/SimpleJavaApp/controller/StudentController.java ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.SimpleJavaApp.controller;
2
+
3
+ import com.example.SimpleJavaApp.entity.Student;
4
+ import com.example.SimpleJavaApp.service.StudentService;
5
+ import org.slf4j.Logger;
6
+ import org.slf4j.LoggerFactory;
7
+ import org.springframework.web.bind.annotation.GetMapping;
8
+ import org.springframework.web.bind.annotation.PathVariable;
9
+ import org.springframework.web.bind.annotation.RequestMapping;
10
+ import org.springframework.web.bind.annotation.RestController;
11
+
12
+ import java.util.List;
13
+
14
+ @RestController
15
+ @RequestMapping("/api/students")
16
+ public class StudentController {
17
+
18
+ private static final Logger logger = LoggerFactory.getLogger(StudentController.class);
19
+
20
+ private final StudentService studentService;
21
+
22
+ public StudentController(StudentService studentService) {
23
+ this.studentService = studentService;
24
+ }
25
+
26
+ @GetMapping
27
+ public List<Student> getAllStudents() {
28
+ logger.info("GET /api/students - Fetching all students");
29
+ List<Student> students = studentService.getAllStudents();
30
+ logger.debug("Returning {} students", students.size());
31
+ return students;
32
+ }
33
+
34
+ @GetMapping("/{id}")
35
+ public Student getStudentById(@PathVariable Long id) {
36
+ logger.info("GET /api/students/{} - Fetching student by id", id);
37
+ Student student = studentService.getStudentById(id);
38
+ logger.debug("Returning student: {}", student.getName());
39
+ return student;
40
+ }
41
+ }
src/main/java/com/example/SimpleJavaApp/entity/Student.java ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.SimpleJavaApp.entity;
2
+
3
+ import jakarta.persistence.Entity;
4
+ import jakarta.persistence.GeneratedValue;
5
+ import jakarta.persistence.GenerationType;
6
+ import jakarta.persistence.Id;
7
+ import jakarta.persistence.Table;
8
+
9
+ @Entity
10
+ @Table(name = "students")
11
+ public class Student {
12
+
13
+ @Id
14
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
15
+ private Long id;
16
+ private Integer rollNo;
17
+ private String name;
18
+ private String className;
19
+ private String fatherName;
20
+
21
+ public Student() {
22
+ }
23
+
24
+ public Student(Integer rollNo, String name, String className, String fatherName) {
25
+ this.rollNo = rollNo;
26
+ this.name = name;
27
+ this.className = className;
28
+ this.fatherName = fatherName;
29
+ }
30
+
31
+ public Long getId() {
32
+ return id;
33
+ }
34
+
35
+ public void setId(Long id) {
36
+ this.id = id;
37
+ }
38
+
39
+ public Integer getRollNo() {
40
+ return rollNo;
41
+ }
42
+
43
+ public void setRollNo(Integer rollNo) {
44
+ this.rollNo = rollNo;
45
+ }
46
+
47
+ public String getName() {
48
+ return name;
49
+ }
50
+
51
+ public void setName(String name) {
52
+ this.name = name;
53
+ }
54
+
55
+ public String getClassName() {
56
+ return className;
57
+ }
58
+
59
+ public void setClassName(String className) {
60
+ this.className = className;
61
+ }
62
+
63
+ public String getFatherName() {
64
+ return fatherName;
65
+ }
66
+
67
+ public void setFatherName(String fatherName) {
68
+ this.fatherName = fatherName;
69
+ }
70
+ }
src/main/java/com/example/SimpleJavaApp/repository/StudentRepository.java ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.SimpleJavaApp.repository;
2
+
3
+ import com.example.SimpleJavaApp.entity.Student;
4
+ import org.springframework.data.jpa.repository.JpaRepository;
5
+ import org.springframework.stereotype.Repository;
6
+
7
+ @Repository
8
+ public interface StudentRepository extends JpaRepository<Student, Long> {
9
+ }
src/main/java/com/example/SimpleJavaApp/service/StudentService.java ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.SimpleJavaApp.service;
2
+
3
+ import com.example.SimpleJavaApp.entity.Student;
4
+ import com.example.SimpleJavaApp.repository.StudentRepository;
5
+ import org.slf4j.Logger;
6
+ import org.slf4j.LoggerFactory;
7
+ import org.springframework.stereotype.Service;
8
+
9
+ import java.util.List;
10
+
11
+ @Service
12
+ public class StudentService {
13
+
14
+ private static final Logger logger = LoggerFactory.getLogger(StudentService.class);
15
+
16
+ private final StudentRepository studentRepository;
17
+
18
+ public StudentService(StudentRepository studentRepository) {
19
+ this.studentRepository = studentRepository;
20
+ }
21
+
22
+ public List<Student> getAllStudents() {
23
+ logger.info("Fetching all students from database");
24
+ List<Student> students = studentRepository.findAll();
25
+ logger.debug("Found {} students", students.size());
26
+ return students;
27
+ }
28
+
29
+ public Student getStudentById(Long id) {
30
+ logger.info("Fetching student with id: {}", id);
31
+ return studentRepository.findById(id)
32
+ .orElseThrow(() -> {
33
+ logger.error("Student not found with id: {}", id);
34
+ return new RuntimeException("Student not found with id: " + id);
35
+ });
36
+ }
37
+ }
src/main/resources/application.properties ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ spring.datasource.url=jdbc:h2:mem:studentdb
2
+ spring.datasource.driverClassName=org.h2.Driver
3
+ spring.datasource.username=sa
4
+ spring.datasource.password=
5
+ spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
6
+ spring.jpa.hibernate.ddl-auto=create-drop
7
+ spring.jpa.show-sql=true
8
+ spring.h2.console.enabled=true
9
+ spring.h2.console.path=/h2-console
10
+ spring.sql.init.mode=always
11
+ spring.jpa.defer-datasource-initialization=true
12
+ spring.jpa.open-in-view=false
13
+ mcp.server.enabled=true
src/main/resources/data.sql ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ INSERT INTO students (roll_no, name, class_name, father_name) VALUES (1, 'Amit Sharma', '10A', 'Rajesh Sharma');
2
+ INSERT INTO students (roll_no, name, class_name, father_name) VALUES (2, 'Priya Patel', '10A', 'Suresh Patel');
3
+ INSERT INTO students (roll_no, name, class_name, father_name) VALUES (3, 'Rahul Kumar', '10B', 'Vijay Kumar');
4
+ INSERT INTO students (roll_no, name, class_name, father_name) VALUES (4, 'Sneha Gupta', '10B', 'Anil Gupta');
5
+ INSERT INTO students (roll_no, name, class_name, father_name) VALUES (5, 'Vikram Singh', '10A', 'Harpreet Singh');