日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

创建一个简单的JAX-RS MessageBodyWriter

發(fā)布時間:2023/12/3 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 创建一个简单的JAX-RS MessageBodyWriter 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

JAX-RS確實(shí)很酷,借助JAXB,只需添加帶有JAXB批注的批注數(shù)據(jù)對象,即可為您轉(zhuǎn)換許多響應(yīng)數(shù)據(jù)類型。 我對JAXB相當(dāng)陌生,但是一些簡單的注釋剪切/粘貼操作將帶給您很長的路要走。

出于無法從JAX-RS資源方法返回該數(shù)據(jù)類型的目的,可能有某些類型的數(shù)據(jù)無法注釋或不會注釋。 一個簡單的示例是返回布爾(原始)或包裝器布爾類。 我在StackOverflow上讀了一個問題,有人問他們是否可以從資源方法返回布爾值,并且由于我不知道答案,所以我決定嘗試一下! 我的版本僅返回XML,而不返回JSON,但您應(yīng)該了解一下。

我從《澤西島用戶指南》的HelloWorld示例開始,然后從那里開始進(jìn)行修改。 我使用了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作為第一個選項(xiàng)。

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之后,以下目標(biāo)是編譯和運(yùn)行項(xiàng)目所需的全部(當(dāng)然!)。

  • mvn compile –編譯代碼
  • mvn exec:java –啟動Grizzly HttpServer并部署Restful服務(wù)。

希望這可以幫助!

參考:從我們的JCG合作伙伴 Mike Miller在Scratching我的編程癢博客上創(chuàng)建一個簡單的JAX-RS MessageBodyWriter 。

翻譯自: https://www.javacodegeeks.com/2014/03/creating-a-simple-jax-rs-messagebodywriter.html

總結(jié)

以上是生活随笔為你收集整理的创建一个简单的JAX-RS MessageBodyWriter的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。