weblogic 10.x 上开发restful服务
之前已经学习过 利用JAX-RS快速开发RESTful 服务,当时是jboss环境,如果原封不动的迁移到weblogic 10.x 版本,会杯具的发现应用启动失败,需要做些小调整:
项目结构如下:
需要在原来的Rest Service(本文中是MyService.java)外,再包装一层,创建一个Application的子类,本例中是 MyApplication.java,代码如下:
1 package yjmyzz.service; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 import javax.ws.rs.ApplicationPath; 7 import javax.ws.rs.core.Application; 8 9 10 import yjmyzz.service.rest.MyService; 11 12 @ApplicationPath("/rest") 13 public class MyApplication extends Application { 14 15 public Set<Class<?>> getClasses() { 16 Set<Class<?>> s = new HashSet<Class<?>>(); 17 s.add(MyService.class); 18 19 return s; 20 } 21 }
MyService.java的内容如下:(这与在jboss环境中的代码完全相同)
1 package yjmyzz.service.rest; 2 3 import javax.ws.rs.GET; 4 5 import javax.ws.rs.Path; 6 7 import javax.ws.rs.Produces; 8 9 import javax.ws.rs.core.MediaType; 10 import javax.xml.bind.JAXBElement; 11 import javax.xml.namespace.QName; 12 13 @Path("/") 14 public class MyService { 15 16 final String XMLNS_NAMESPACE = "http://yjmyzz.cnblogs.com/rest/service"; 17 final String ROOT_NODE = "root"; 18 19 @GET 20 @Path("/json/hello") 21 @Produces(MediaType.APPLICATION_JSON) 22 public JAXBElement<String> getHelloWorldJSON() { 23 JAXBElement<String> result = new JAXBElement<String>(new QName("", 24 ROOT_NODE), String.class, sayHelloWorld()); 25 return result; 26 } 27 28 @GET 29 @Path("/xml/hello") 30 @Produces(MediaType.APPLICATION_XML) 31 public JAXBElement<String> getHelloWorldXML() { 32 JAXBElement<String> result = new JAXBElement<String>(new QName( 33 XMLNS_NAMESPACE, ROOT_NODE), String.class, sayHelloWorld()); 34 return result; 35 } 36 37 private String sayHelloWorld() { 38 return "Hello JAX-RS-1"; 39 } 40 41 }
还有一个关键步骤:在webapp/WEB-INF目录下创建web.xml,内容参考如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd " 4 version="2.5"> 5 6 <servlet> 7 <servlet-name>MyService</servlet-name> 8 <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 9 <init-param> 10 <param-name>javax.ws.rs.Application</param-name> 11 <param-value>yjmyzz.service.MyApplication </param-value> 12 </init-param> 13 </servlet> 14 15 <servlet-mapping> 16 <servlet-name>MyService</servlet-name> 17 <url-pattern>/rest/*</url-pattern> 18 </servlet-mapping> 19 20 </web-app>
最后附上pom.xml的内容:
1 <?xml version="1.0"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>org.jboss.as.quickstarts</groupId> 8 <artifactId>helloworld-rs</artifactId> 9 <version>7.1.2-SNAPSHOT</version> 10 <packaging>war</packaging> 11 <name>JBoss EAP Quickstart: helloworld-rs</name> 12 <description>JBoss EAP Quickstart: Helloworld using JAX-RS</description> 13 14 <properties> 15 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 16 <version.jboss.maven.plugin>7.4.Final</version.jboss.maven.plugin> 17 <version.jboss.spec.javaee.6.0>3.0.2.Final</version.jboss.spec.javaee.6.0> 18 <version.war.plugin>2.1.1</version.war.plugin> 19 <maven.compiler.target>1.6</maven.compiler.target> 20 <maven.compiler.source>1.6</maven.compiler.source> 21 </properties> 22 23 <dependencyManagement> 24 <dependencies> 25 <dependency> 26 <groupId>org.jboss.spec</groupId> 27 <artifactId>jboss-javaee-6.0</artifactId> 28 <version>${version.jboss.spec.javaee.6.0}</version> 29 <type>pom</type> 30 <scope>import</scope> 31 </dependency> 32 </dependencies> 33 </dependencyManagement> 34 35 <dependencies> 36 <dependency> 37 <groupId>com.sun.jersey</groupId> 38 <artifactId>jersey-bundle</artifactId> 39 <version>1.15</version> 40 </dependency> 41 </dependencies> 42 43 <build> 44 <finalName>${project.artifactId}</finalName> 45 <plugins> 46 <plugin> 47 <artifactId>maven-war-plugin</artifactId> 48 <version>${version.war.plugin}</version> 49 <configuration> 50 <failOnMissingWebXml>true</failOnMissingWebXml> 51 </configuration> 52 </plugin> 53 54 <plugin> 55 <groupId>org.jboss.as.plugins</groupId> 56 <artifactId>jboss-as-maven-plugin</artifactId> 57 <version>${version.jboss.maven.plugin}</version> 58 </plugin> 59 </plugins> 60 </build> 61 62 63 </project>
ok,打完收工!
附示例源码下载:weblogic-rest-helloworld.zip
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。