jax-rs jax-ws_创建一个简单的JAX-RS MessageBodyWriter
jax-rs jax-ws
JAX-RS確實很棒,借助JAXB,只需添加帶有JAXB批注的批注數(shù)據(jù)對象,即可為您轉(zhuǎn)換許多響應(yīng)數(shù)據(jù)類型。 我對JAXB相當陌生,但是一些簡單的注釋的剪切/粘貼操作將帶給您很多幫助。
出于無法從JAX-RS資源方法返回該數(shù)據(jù)類型的目的,可能有某些類型的數(shù)據(jù)無法注釋或不會注釋。 一個簡單的示例是返回布爾(原始)或包裝器布爾類。 我在StackOverflow上讀了一個問題,有人問他們是否可以從資源方法返回布爾值,由于我不知道答案,所以我決定嘗試一下! 我的版本僅返回XML,而不返回JSON,但您應(yīng)該明白這一點。
我從《澤西島用戶指南》的HelloWorld示例開始,然后從那里開始進行修改。 我使用了pom.xml,唯一的變化是取消注釋了一個塊以允許使用JSON。
主班
該類來自Hello World示例,沒有任何更改。
package com.example;import org.glassfish.grizzly.http.server.HttpServer; import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; import org.glassfish.jersey.server.ResourceConfig;import java.io.IOException; import java.net.URI;/*** Main class.**/ public class Main {// Base URI the Grizzly HTTP server will listen onpublic static final String BASE_URI = "http://localhost:8080/myapp/";/*** Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.* @return Grizzly HTTP server.*/public static HttpServer startServer() {// create a resource config that scans for JAX-RS resources and providers// in com.example packagefinal ResourceConfig rc = new ResourceConfig().packages("com.example");// create and start a new instance of grizzly http server// exposing the Jersey application at BASE_URIreturn GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);}/*** Main method.* @param args* @throws IOException*/public static void main(String[] args) throws IOException {final HttpServer server = startServer();System.out.println(String.format("Jersey app started with WADL available at "+ "%sapplication.wadl\nHit enter to stop it...", BASE_URI));System.in.read();server.stop();} }資源類別
我創(chuàng)建了一個資源類,其中包括一個GET方法返回一個布爾值,另一個GET方法返回包裝布爾值類。 注意,getBool()和getBoolean()方法將XML作為第一個選項。
package com.example;import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType;/*** Root resource (exposed at "myresource" path)*/ @Path("myresource") public class MyResource {/*** Method handling HTTP GET requests. The returned object will be sent* to the client as "text/plain" media type.** @return String that will be returned as a text/plain response.*/@GET@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN})public String getIt() {return "Got it!";}@GET@Path("/bool")@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN})public boolean getBool() {return false;}@GET@Path("/Boolean")@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN})public Boolean getBoolean() {return Boolean.TRUE;} }BooleanMessageBodyWriter類
這是有趣的部分,創(chuàng)建MessageBodyWriter類以允許資源方法返回布爾值或布爾值的XML。
package com.example;import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.ext.MessageBodyWriter; import javax.ws.rs.ext.Provider; import javax.ws.rs.WebApplicationException; import java.io.IOException; import java.io.InputStream; import java.io.DataOutputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Serializable; import java.lang.annotation.Annotation; import java.lang.reflect.Type;@Provider @Produces("application/xml") public class BooleanMessageBodyWriter implements MessageBodyWriter{@Overridepublic boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {System.out.println("isWriteable called...");return type == Boolean.class;}@Overridepublic long getSize(Boolean myBool, Class type, Type genericType,Annotation[] annotations, MediaType mediaType) {// deprecated by JAX-RS 2.0 and ignored by Jersey runtimereturn 0;}@Overridepublic void writeTo(Boolean myBool,Class type,Type genericType,Annotation[] annotations,MediaType mediaType,MultivaluedMaphttpHeaders,OutputStream entityStream)throws IOException, WebApplicationException {StringBuilder sb = new StringBuilder();sb.append("").append(myBool.toString()).append("");DataOutputStream dos = new DataOutputStream(entityStream);dos.writeUTF(sb.toString());} }我以前沒有使用過Maven,但是在安裝maven之后,以下目標是編譯和運行項目所需的全部(當然!)。
- mvn compile –編譯代碼
- mvn exec:java –啟動Grizzly HttpServer并部署Restful服務(wù)。
希望這可以幫助!
翻譯自: https://www.javacodegeeks.com/2014/03/creating-a-simple-jax-rs-messagebodywriter.html
jax-rs jax-ws
總結(jié)
以上是生活随笔為你收集整理的jax-rs jax-ws_创建一个简单的JAX-RS MessageBodyWriter的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 屏幕乱跳广告解决方法(屏幕乱跳广告解决方
- 下一篇: 曹魏的首都在哪里(三国时期曹魏的都城在什