JAX RS – 30 – Client API – 03 – HTTP POST XML

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.

Project.java

package _24.client.api.post.xml.model;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "project")
@XmlAccessorType(XmlAccessType.FIELD)
public class Project {

	@XmlElement
	private int id;
	@XmlElement
	private String name;
	@XmlElement
	private String department;

	//constructors
	//getters and setters
	//toString
	...
}

ProjectResource.java

package _24.client.api.post.xml.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 _24.client.api.post.xml.model.Project;

@Path("/project-client-api")
public class ProjectResource {

	@POST
	@Path("/addProject")
	@Consumes(MediaType.APPLICATION_XML)
	public String addProject(Project project) {
		System.out.println("ProjectResource#addProject");
		System.out.println(project);
		return "addProject#succeed";
	}

	@POST
	@Path("/addProjectBatch")
	@Consumes(MediaType.APPLICATION_XML)
	public String addProjectBatch(List<Project> projectList) {
		System.out.println("ProjectResource#addProjectBatch");
		System.out.println(projectList);
		return "addProjectBatch#succeed";
	}
}

Test sinifimizi yazalim …

Onceki bolumde oldugu gibi WebTarget objemizi olusturuyoruz.
Entity objesi icin Entity.xml metodundan yararlaniyoruz.

ProjectClientAPITest1.java

package _24.client.api.post.xml.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 _24.client.api.post.xml.model.Project;

public class ProjectClientAPITest1 {

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

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

		Entity<Project> xmlEntity = Entity.xml(new Project(1, "IT", "Payment Project"));

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

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

		client.close();
	}
}

Ornegimizi calistirdigimizda ;

Eclipse Application Console’da

status : 200
readEntity: addProject#succeed

Tomcat Console’da

ProjectResource#addProject
Project [id=1, name=IT, department=Payment Project]

Bir baska ornek olarak ;

ProjectClientAPITest2.java

package _24.client.api.post.xml.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.GenericEntity;
import javax.ws.rs.core.Response;

import _24.client.api.post.xml.model.Project;

public class ProjectClientAPITest2 {

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

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

		List>Project> projects = Arrays.asList(new Project(1, "IT", "Payment Project"),
				new Project(2, "IT", "Security Project"));

		GenericEntity>List>Project>> projectEntityList = new GenericEntity>List>Project>>(projects) {};

		Entity>GenericEntity>List>Project>>> xmlEntity = Entity.xml(projectEntityList);

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

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

		client.close();
	}
}

Ornegimizi calistirdigimizda;

status : 200
readEntity: addProjectBatch#succeed
ProjectResource#addProjectBatch
ProjectList [projectList=[Project [id=1, name=IT, department=Payment Project], Project [id=2, name=IT, department=Security Project]]]

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 *