懂福溶州做戒网站,模板 网站 教程,连锁品牌网站建设,视频模板网JAX-RS确实很酷#xff0c;借助JAXB#xff0c;只需添加带有JAXB批注的批注数据对象#xff0c;即可为您转换许多响应数据类型。 我对JAXB相当陌生#xff0c;但是一些简单的注释剪切/粘贴操作将带给您很长的路要走。 出于无法从JAX-RS资源方法返回该数据类型的目的#… JAX-RS确实很酷借助JAXB只需添加带有JAXB批注的批注数据对象即可为您转换许多响应数据类型。 我对JAXB相当陌生但是一些简单的注释剪切/粘贴操作将带给您很长的路要走。 出于无法从JAX-RS资源方法返回该数据类型的目的可能有某些类型的数据无法注释或不会注释。 一个简单的示例是返回布尔原始或包装器布尔类。 我在StackOverflow上读了一个问题有人问他们是否可以从资源方法返回布尔值并且由于我不知道答案所以我决定尝试一下 我的版本仅返回XML而不返回JSON但您应该了解一下。 我从《泽西岛用户指南》的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();}
}资源类别 我创建了一个资源类其中包括一个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.*/GETProduces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN})public String getIt() {return Got it!;}GETPath(/bool)Produces({MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN})public boolean getBool() {return false;}GETPath(/Boolean)Produces({MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN})public Boolean getBoolean() {return Boolean.TRUE;}
}BooleanMessageBodyWriter类 这是有趣的部分创建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 execjava –启动Grizzly HttpServer并部署Restful服务。 希望这可以帮助 参考从我们的JCG合作伙伴 Mike Miller在Scratching我的编程痒博客上创建一个简单的JAX-RS MessageBodyWriter 。 翻译自: https://www.javacodegeeks.com/2014/03/creating-a-simple-jax-rs-messagebodywriter.html