Spring ile Restful WebServis kullanımı
Merhaba arkadaşlar ,
Spring ile rest servisler nasıl kullanılıyor ona değineceğiz. Projemizi Maven ile açıyoruz ve gerekli kütüphaneleri pom.xml dosyamıza ekliyoruz.
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
<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 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>io.bilisim</groupId> <artifactId>SpringRestFul</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>SpringRestFul Json Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>3.2.2.RELEASE</spring.version> <jackson.version>1.9.10</jackson.version> <jdk.version>1.6</jdk.version> </properties> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>1.7.1</version> </dependency> </dependencies> <build> <finalName>SpringRestFul</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <version>2.9</version> <configuration> <downloadSources>true</downloadSources> <downloadJavadocs>false</downloadJavadocs> <wtpversion>2.0</wtpversion> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> </plugins> </build> </project> |
Varsayalım ki Company isiminde bir modelimiz olsun .
Company.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package io.bilisim.model; public class Company { String name; String departments[]; public String getName() { return name; } public void setName(String name) { this.name = name; } public String[] getDepartments() { return departments; } public void setDepartments(String[] departments) { this.departments = departments; } public Company() { } } |
Rest Servis methodlarını controller sınıfımıza yazalım ve notasyonlar ile servisimizi oluşturalım.
CompanyController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package io.bilisim.controller; import javax.ws.rs.Produces; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.turkishh.model.Company; import com.turkishh.utils.MyUtil; @Controller //Uygulama dizininden sonra servis path'imizi yazıyoruz. @RequestMapping("/service") public class CompanyController { //Servis path'imizden sonra hangi servis methoduna gidiceğini belirliyoruz. //ve {name} oalrak verdiğimiz referans ise request'te geleceğimiz parametredir @RequestMapping(value = "/company/{name}", method = RequestMethod.GET) @Produces("application/json") public @ResponseBody String getCompany(@PathVariable String name) { Company copmany = new Company(); copmany.setName(name); copmany.setDepartments(new String[] { "Software", "Accounting", "Security", "Hardware" }); //Nesneyi json'a çevirip dönüyoruz. return MyUtil.objectToJson(copmany); } } |
Objemizi jsona parse eden methodumuzu yazıyoruz.
MyUtil.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package io.bilisim.utils; import com.google.gson.Gson; public class MyUtil { static Gson gson; public static String objectToJson(Object src) { //parse eden kütüphane gson = new Gson(); return gson.toJson(src); } } |
Şimdi ise spring konfigurasyonlarını yapıyoruz.
applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="io.bilisim.controller" /> <mvc:annotation-driven /> </beans> |
Son olarak web.xml dosyamızdaki ayarlamalarımızı yaparak teste başlıyoruz.
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Spring Web RestFul Services Application</display-name> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> //RestFul servis path'i nerden başlıyacağına karar verebiliryoruz <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> //Spring konfigurasyonları <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> |
Uygulamamızı sunucumuza deploy ettikten sonra şu dizinde yer almış olacaktır.
http://localhost:8080/SpringRestFul/service/company/{set_edilecek_parametre}
Request attığımızda çıktısı aşşağıdaki gibi olmalıdır.
Umarım yararlı olmuştur .
İyi Çalışmalar.