JAX RS – 19 – Standard Entity Providers
Merhaba Arkadaslar
Bu bolumde Standard Entity Provider’lari inceleyecegiz.
Orneklerimizde java.lang.String donus tipini kullandik bunun disinda kullanabilecegimiz bazi donus tiplerini inceleyelim ;
javax.ws.rs.core.StreamingOutput
public interface StreamingOutput { void write(OutputStream output) throws IOException, WebApplicationException; }
ProviderResource.java
StreamingOutput interface’inden yararlanabiliriz ;
Anonymous class olarak implementasyon yapabilecegimiz gibi Java 8 de gelen Lamda expression da kullanabiliriz.
Bir baska nokta olarak @Produces annotation ekledik.
//imports @Path("/provider") public class ProviderResource { @GET @Produces(MediaType.TEXT_PLAIN) @Path("/streamingOutput") public StreamingOutput streamingOutput() { // StreamingOutput sOutput = new StreamingOutput() { // @Override // public void write(OutputStream os) throws IOException, WebApplicationException { // String message = "streamingOutput isCalled."; // os.write(message.getBytes()); // } // }; // return sOutput; //Java8 lambda String message = "streamingOutput isCalled."; StreamingOutput sOutput = (OutputStream os) -> os.write(message.getBytes()); return sOutput; } }
byte[]
@GET @Produces(MediaType.TEXT_PLAIN) @Path("/byteArray") public byte[] byteArray() { String message = "byteArray is called"; return message.getBytes(); }
java.lang.String
Simdiye kadar donus tipimiz genel olarak java.lang.String sinifinda oldu.
Burada dikkat edecegimiz nokta @Produces annotation da MediaType.APPLICATION_XML veriyoruz.
Boylece XML formatina uygun datayi dondugumuzde bunu uygun formatta gorebiliriz.
@GET @Produces(MediaType.APPLICATION_XML) @Path("/stringXMLResponse") public String stringXMLResponse() { String message = "<customer><name>Levent Erguder</name></customer>"; return message; }
int
@GET @Produces(MediaType.TEXT_PLAIN) @Path("/intResponse") // http://localhost:8080/injavawetrust.resteasy.tutorial/provider/intResponse public int intResponse() { // return 100; }
boolean
@GET @Produces(MediaType.TEXT_PLAIN) @Path("/booleanResponse") // http://localhost:8080/injavawetrust.resteasy.tutorial/provider/booleanResponse public boolean booleanResponse() { // return true; }
java.io.File
Bir baska ornek olarak java.io.File sinifi donus tipi olabilir.
@GET @Path("/file") @Produces(MediaType.TEXT_PLAIN) public File getFile() throws URISyntaxException { URI uri = getClass().getClassLoader().getResource("file.txt").toURI(); File file = new File(uri); return file; }
java.io.InputStream
@GET @Path("/inputStream") @Produces(MediaType.TEXT_PLAIN) // http://localhost:8080/injavawetrust.resteasy.tutorial/provider/inputStream public InputStream getFileInputStream() throws FileNotFoundException, URISyntaxException { URI uri = getClass().getClassLoader().getResource("file.txt").toURI(); File file = new File(uri); FileInputStream is = new FileInputStream(file); return is; }
ProviderResource.java
package _13.standardEntityProvider.service; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.OutputStream; import java.net.URI; import java.net.URISyntaxException; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.StreamingOutput; @Path("/provider") public class ProviderResource { @GET @Produces(MediaType.TEXT_PLAIN) @Path("/streamingOutput") // http://localhost:8080/injavawetrust.resteasy.tutorial/provider/streamingOutput public StreamingOutput streamingOutput() { // StreamingOutput sOutput = new StreamingOutput() { // @Override // public void write(OutputStream os) throws IOException, WebApplicationException { // String message = "streamingOutput isCalled."; // os.write(message.getBytes()); // } // }; // return sOutput; // Java8 lambda String message = "streamingOutput isCalled."; StreamingOutput sOutput = (OutputStream os) -> os.write(message.getBytes()); return sOutput; } @GET @Produces(MediaType.TEXT_PLAIN) @Path("/byteArray") // http://localhost:8080/injavawetrust.resteasy.tutorial/provider/byteArray public byte[] byteArray() { String message = "byteArray is called"; return message.getBytes(); } @GET @Produces(MediaType.APPLICATION_XML) @Path("/stringXMLResponse") // http://localhost:8080/injavawetrust.resteasy.tutorial/provider/stringXMLResponse public String stringXMLResponse() { String message = "<customer><name>Levent Erguder
javax.ws.rs.core.StreamingOutput
http://localhost:8080/injavawetrust.jersey.tutorial/provider/streamingOutput http://localhost:8080/injavawetrust.resteasy.tutorial/provider/streamingOutput
byte[]
http://localhost:8080/injavawetrust.jersey.tutorial/provider/byteArray http://localhost:8080/injavawetrust.resteasy.tutorial/provider/byteArray
java.lang.String
http://localhost:8080/injavawetrust.jersey.tutorial/provider/stringXMLResponse http://localhost:8080/injavawetrust.resteasy.tutorial/provider/stringXMLResponse
int
http://localhost:8080/injavawetrust.jersey.tutorial/provider/intResponse http://localhost:8080/injavawetrust.resteasy.tutorial/provider/intResponse
boolean
http://localhost:8080/injavawetrust.jersey.tutorial/provider/booleanResponse http://localhost:8080/injavawetrust.resteasy.tutorial/provider/booleanResponse
java.io.File
http://localhost:8080/injavawetrust.jersey.tutorial/provider/file http://localhost:8080/injavawetrust.resteasy.tutorial/provider/file
java.io.InputStream
http://localhost:8080/injavawetrust.jersey.tutorial/provider/inputStream http://localhost:8080/injavawetrust.resteasy.tutorial/provider/inputStream
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
Leave a Reply