Skip to content

Commit b9880a3

Browse files
authored
commit
1 parent 99375bc commit b9880a3

85 files changed

Lines changed: 2249 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<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">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.mc_es</groupId>
4+
<artifactId>003-PathVariable</artifactId>
5+
<version>1.0</version>
6+
<packaging>war</packaging>
7+
<properties>
8+
<spring-webmvc.version>5.3.23</spring-webmvc.version>
9+
<jackson-databind.version>2.13.4</jackson-databind.version>
10+
<javax.servelet-api.version>4.0.1</javax.servelet-api.version>
11+
<javax.servlet.jsp-api.version>2.3.3</javax.servlet.jsp-api.version>
12+
<javax.annotation-api.version>1.3.2</javax.annotation-api.version>
13+
</properties>
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework</groupId>
17+
<artifactId>spring-webmvc</artifactId>
18+
<version>${spring-webmvc.version}</version>
19+
</dependency>
20+
21+
<dependency>
22+
<groupId>com.fasterxml.jackson.core</groupId>
23+
<artifactId>jackson-databind</artifactId>
24+
<version>${jackson-databind.version}</version>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>javax.servlet</groupId>
29+
<artifactId>javax.servlet-api</artifactId>
30+
<version>${javax.servelet-api.version}</version>
31+
<scope>provided</scope>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>javax.servlet.jsp</groupId>
36+
<artifactId>javax.servlet.jsp-api</artifactId>
37+
<version>${javax.servlet.jsp-api.version}</version>
38+
<scope>provided</scope>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>javax.annotation</groupId>
43+
<artifactId>javax.annotation-api</artifactId>
44+
<version>${javax.annotation-api.version}</version>
45+
</dependency>
46+
</dependencies>
47+
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<artifactId>maven-compiler-plugin</artifactId>
52+
<version>3.8.1</version>
53+
<configuration>
54+
<release>18</release>
55+
</configuration>
56+
</plugin>
57+
<plugin>
58+
<artifactId>maven-war-plugin</artifactId>
59+
<version>3.2.3</version>
60+
</plugin>
61+
</plugins>
62+
</build>
63+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.mc_es.spring.config;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
6+
7+
@Configuration
8+
@EnableWebMvc
9+
@ComponentScan(basePackages = "com.mc_es.spring")
10+
public class AppConfig {
11+
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.mc_es.spring.config;
2+
3+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
4+
5+
public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
6+
7+
@Override
8+
protected Class<?>[] getRootConfigClasses() {
9+
return null;
10+
}
11+
12+
@Override
13+
protected Class<?>[] getServletConfigClasses() {
14+
return new Class[] { AppConfig.class };
15+
}
16+
17+
@Override
18+
protected String[] getServletMappings() {
19+
return new String[] { "/" };
20+
}
21+
22+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.mc_es.spring.entitiy;
2+
3+
public class Student {
4+
private int id;
5+
private String firstName;
6+
private String lastName;
7+
private int age;
8+
9+
public Student() {
10+
11+
}
12+
13+
public Student(int id, String firstName, String lastName, int age) {
14+
this.id = id;
15+
this.firstName = firstName;
16+
this.lastName = lastName;
17+
this.age = age;
18+
}
19+
20+
public int getId() {
21+
return id;
22+
}
23+
24+
public void setId(int id) {
25+
this.id = id;
26+
}
27+
28+
public String getFirstName() {
29+
return firstName;
30+
}
31+
32+
public void setFirstName(String firstName) {
33+
this.firstName = firstName;
34+
}
35+
36+
public String getLastName() {
37+
return lastName;
38+
}
39+
40+
public void setLastName(String lastName) {
41+
this.lastName = lastName;
42+
}
43+
44+
public int getAge() {
45+
return age;
46+
}
47+
48+
public void setAge(int age) {
49+
this.age = age;
50+
}
51+
52+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.mc_es.spring.rest;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import javax.annotation.PostConstruct;
7+
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.PathVariable;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
import com.mc_es.spring.entitiy.Student;
14+
15+
@RestController
16+
@RequestMapping(value = "/api")
17+
public class StudentRestController {
18+
private List<Student> students;
19+
20+
// bir nesne olusturulduktan hemen sonra herhangi uygulanan bir metoda giris
21+
// saglamasi ve o metodu isletmesidir.
22+
// @PostConstruct metodu yalnizca metod baslarinda uygulanabilen bir notasyon
23+
// turudur.
24+
@PostConstruct
25+
public void load() {
26+
students = new ArrayList<Student>();
27+
students.add(new Student(0, "Can", "Eser", 21));
28+
students.add(new Student(1, "Emirhan", "Eser", 13));
29+
students.add(new Student(2, "Furkan", "Eser", 19));
30+
}
31+
32+
@GetMapping(value = "/students")
33+
public List<Student> getStudents() {
34+
return students;
35+
}
36+
37+
// @PathVariable ve @RequestParam anotasyonlari gonderilen istegin icindeki
38+
// parametrelerdir.
39+
// Ortak yonu her ikisininde parametre olmasidir.
40+
// Eger elimizde bir url template varsa bu durumda @PathVariable kullanimi daha
41+
// uygundur.
42+
// Diger durumlar icinse @RequestParam daha kullanislidir.
43+
// ornekler:
44+
45+
// http://localhost:8080/movies -- (/movies)
46+
// bu istek tum filmlerin listesini doner
47+
48+
// http://localhost:8080/movies/1 -- (/movies/{moviesId})
49+
// bu istek id degeri 1 olan filmi doner
50+
51+
// http://localhost:8080/movies/1/stars --(/movies/{movieId}/stars)
52+
// bu istek id degeri 1 oan filmin oyuncularinin listesini doner
53+
54+
// http://localhost:8080/movies/1/stars/2 --(/movies/1/stars/2)
55+
// bu istek id degeri 1 olan filmin oyuncularindan id degeri 2 olani doner
56+
57+
// dikkat ettiyseniz, baglanti adresi bir template halindedir. Toplamda 4 metot
58+
// mevcuttur.
59+
60+
// @RequestParam ornegi inceliyelim:
61+
// http://localhost:8080/movies/search
62+
// filtresi tum kaynagi getirir
63+
64+
// http://localhost:8080/movies/search/?name=saw
65+
// istedigimiz parametreleri zorunlu veya istege bagli yapabilecegimiz gibi
66+
// onlara default deger de verebiliriz
67+
68+
// http://localhost:8080/movies/search?name=saw&country=abd
69+
// filmin adi saw olan ve ABD yapimi olan filmleri getir
70+
@GetMapping(value = "/students/{studentId}")
71+
public Student getStudentById(@PathVariable(name = "studentId") int studentId) {
72+
return this.students.get(studentId);
73+
}
74+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Class-Path:
3+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<%@ page language="java" contentType="text/html; charset=UTF-8"
2+
pageEncoding="UTF-8"%>
3+
<!DOCTYPE html>
4+
<html>
5+
<head>
6+
<meta charset="UTF-8">
7+
<title>Insert title here</title>
8+
<style type="text/css">
9+
10+
a {
11+
font-size: 20px;
12+
}
13+
a:hover {
14+
color: red;
15+
}
16+
</style>
17+
</head>
18+
19+
<body>
20+
<h2 id="back">Path Variable Annotation</h2>
21+
<a href="${pageContext.request.contextPath}/api/students">Get all students</a>
22+
<br>
23+
<br>
24+
<a href="${pageContext.request.contextPath}/api/students/0">Get student by id</a>
25+
</body>
26+
</html>
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)