JAX RS – 29 – Client API – 02 – HTTP POST JSON

Merhaba Arkadaslar
Bu bolumde JAX-RS Client API’yi incelemeye devam edecegiz, HTTP POST orneklerini uygulayacagiz.
Her zamanki gibi RESTEasy ve Jersey projelerimiz uzerinde implementation’lari yapacagiz.
Ilgili projeleri Github uzerinden takip edebilirsiniz.

Employee.java

package _23.client.api.post.json.model;

public class Employee {

	private int id;
	private String name;
	private String surname;

	public Employee() {
		super();
	}

	public Employee(int id, String name, String surname) {
		super();
		this.id = id;
		this.name = name;
		this.surname = surname;
	}

	//getters and setters
	//toString

}

EmployeeResource.java

package _23.client.api.post.json.service;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;

import _23.client.api.post.json.model.Employee;

@Path("/employee-client-api")
public class EmployeeResource {

	@POST
	@Path("/addEmployee")
	@Consumes(MediaType.APPLICATION_JSON)
	public String addEmployee(Employee employee) {
		System.out.println("EmployeeResource#addEmployee");
		System.out.println(employee);
		return "addEmployee#succeed";
	}

	@POST
	@Path("/addEmployeeBatch")
	@Consumes(MediaType.APPLICATION_JSON)
	public String addEmployeeBatch(List<Employee> employees) {
		System.out.println("EmployeeResource#addEmployeeBatch");
		System.out.println(employees);
		return "addEmployeeBatch#succeed";
	}
}

Client Test siniflarimizi yazmaya baslayalim;

EmployeeClientAPITest1.java
Ornek test sinifimizi inceleyelim ;
Oncelikle addEmployee metodununu cagirmak icin ilgili URI adresini belirtiyoruz ve WebTarget objesini olusturuyoruz.

addEmployee metodu Consumer olarak JSON kullanmakta , (@Consumes(MediaType.APPLICATION_JSON) )
Entity.json metodunu kullaniyoruz ve Employee objesini veriyoruz.

Sonrasinda Builder objesi uzerinden HTTP POST istegi yapiyoruz.
status ve addEmployee metoundan donen String degeri test amacli yazdiriyoruz.

package _23.client.api.post.json.test;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

import _23.client.api.post.json.model.Employee;

public class EmployeeClientAPITest1 {

	public static void main(String[] args) {
		String uri = "http://localhost:8080/injavawetrust.resteasy.tutorial/employee-client-api/addEmployee";

		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(uri);

		Entity<Employee> jsonEntity = Entity.json(new Employee(1, "Levent", "Erguder"));

		// valid too
		// Entity<Employee> jsonEntity = Entity.entity(new Employee(1, "Levent",
		// "Erguder"),MediaType.APPLICATION_JSON);

		Invocation.Builder builder = target.request();
		Response message = builder.post(jsonEntity);


		System.out.println("status : " + message.getStatus());
		System.out.println("readEntity: " + message.readEntity(String.class));

		client.close();
	}
}

Test sinifimizi calistirdigimizda;
Eclipse Console’da

status : 200
readEntity: addEmployee#succeed

EmployeeResource#addEmployee metodu calisacaktir , Tomcat Console’da

EmployeeResource#addEmployee
Employee [id=1, name=Levent, surname=Erguder]

Bir baska ornek olarak ;
Burada da addEmployeeBatch metoduna HTTP POST istegi yapiyoruz.
Burada Entity.json metoduna List<Employee> metodunu vermekteyiz.

EmployeeClientAPITest2.java

package _23.client.api.post.json.test;

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

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

import _23.client.api.post.json.model.Employee;

public class EmployeeClientAPITest2 {

	public static void main(String[] args) {
		String uri = "http://localhost:8080/injavawetrust.resteasy.tutorial/employee-client-api/addEmployeeBatch";

		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(uri);

		List<Employee> employees = Arrays.asList(new Employee(1, "Levent", "Erguder"),
				new Employee(2, "James", "Gosling"));

		Entity<List<Employee>> jsonEntity = Entity.json(employees);

		Invocation.Builder builder = target.request();
		Response message = builder.post(jsonEntity);

		System.out.println("status : " + message.getStatus());
		System.out.println("readEntity: " + message.readEntity(String.class));

		client.close();
	}
}

Ornegimizi calistirdigimizda Eclipse Console’da

status : 200
readEntity: addEmployeeBatch#succeed

Tomcat Console’da;

EmployeeResource#addEmployeeBatch
[Employee [id=1, name=Levent, surname=Erguder], Employee [id=2, name=James, surname=Gosling]]

Github kaynak kodlar / source folder
injavawetrust.resteasy
injavawetrust.jersey

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *