之前已经学习过 利用JAX-RS快速开发RESTful 服务,当时是jboss环境,如果原封不动的迁移到weblogic 10.x 版本,会杯具的发现应用启动失败,需要做些小调整:
项目结构如下:
需要在原来的Rest Service(本文中是MyService.java)外,再包装一层,创建一个Application的子类,本例中是 MyApplication.java,代码如下:
1packageyjmyzz.service;
2
3importjava.util.HashSet;
4importjava.util.Set;
5
6importjavax.ws.rs.ApplicationPath;
7importjavax.ws.rs.core.Application;
8
9
10importyjmyzz.service.rest.MyService;
11
12@ApplicationPath(/rest)
13publicclassMyApplicationextendsApplication{
14
15publicSetClass?getClasses(){
16SetClass?s=newHashSetClass?();
17s.add(MyService.class);
18
19returns;
20}
21}
MyService.java的内容如下:(这与在jboss环境中的代码完全相同)
1packageyjmyzz.service.rest;
2
3importjavax.ws.rs.GET;
4
5importjavax.ws.rs.Path;
6
7importjavax.ws.rs.Produces;
8
9importjavax.ws.rs.core.MediaType;
10importjavax.xml.bind.JAXBElement;
11importjavax.xml.namespace.QName;
12
13@Path(/)
14publicclassMyService{
15
16finalStringXMLNS_NAMESPACE=http://yjmyzz.cnblogs.com/rest/service;
17finalStringROOT_NODE=root;
18
19@GET
20@Path(/json/hello)
21@Produces(MediaType.APPLICATION_JSON)
22publicJAXBElementStringgetHelloWorldJSON(){
23JAXBElementStringresult=newJAXBElementString(newQName(,
24ROOT_NODE),String.class,sayHelloWorld());
25returnresult;
26}
27
28@GET
29@Path(/xml/hello)
30@Produces(MediaType.APPLICATION_XML)
31publicJAXBElementStringgetHelloWorldXML(){
32JAXBElementStringresult=newJAXBElementString(newQName(
33XMLNS_NAMESPACE,ROOT_NODE),String.class,sayHelloWorld());
34returnresult;
35}
36
37privateStringsayHelloWorld(){
38returnHelloJAX-RS-1;
39}
40
41}
还有一个关键步骤:在webapp/WEB-INF目录下创建web.xml,内容参考如下:
1?xmlversion=1.0encoding=UTF-8?
2web-appxmlns=http://java.sun.com/xml/ns/javaeexmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
3xsi:schemaLocation=http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
4version=2.5
5
6servlet
7servlet-nameMyService/servlet-name
8servlet-classcom.sun.jersey.spi.container.servlet.ServletContainer/servlet-class
9init-param
10param-namejavax.ws.rs.Application/param-name
11param-valueyjmyzz.service.MyApplication/param-value
12/init-param
13/servlet
14
15servlet-mapping
16servlet-nameMyService/servlet-name
17url-pattern/rest/*/url-pattern
18/servlet-mapping
19
20/web-app
最后附上pom.xml的内容:
1?xmlversion=1.0?
2
3projectxmlns=http://maven.apache.org/POM/4.0.0xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
4xsi:schemaLocation=http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd
5modelVersion4.0.0/modelVersion
6
7groupIdorg.jboss.as.quickstarts/groupId
8artifactIdhelloworld-rs/artifactId
9version7.1.2-SNAPSHOT/version
10packagingwar/packaging
11nameJBossEAPQuickstart:helloworld-rs/name
12descriptionJBossEAPQuickstart:HelloworldusingJAX-RS/description
13
14properties
15project.build.sourceEncodingUTF-8/project.build.sourceEncoding
16version.jboss.maven.plugin7.4.Final/version.jboss.maven.plugin
17version.jboss.spec.javaee.6.03.0.2.Final/version.jboss.spec.javaee.6.0
18version.war.plugin2.1.1/version.war.plugin
19maven.compiler.target1.6/maven.compiler.target
20maven.compiler.source1.6/maven.compiler.source
21/properties
22
23dependencyManagement
24dependencies
25dependency
26groupIdorg.jboss.spec/groupId
27artifactIdjboss-javaee-6.0/artifactId
28version${version.jboss.spec.javaee.6.0}/version
29typepom/type
30scopeimport/scope
31/dependency
32/dependencies
33/dependencyManagement
34
35dependencies
36dependency
37groupIdcom.sun.jersey/groupId
38artifactIdjersey-bundle/artifactId
39version1.15/version
40/dependency
41/dependencies
42
43build
44finalName${project.artifactId}/finalName
45plugins
46plugin
47artifactIdmaven-war-plugin/artifactId
48version${version.war.plugin}/version
49configuration
50failOnMissingWebXmltrue/failOnMissingWebXml
51/configuration
52/plugin
53
54plugin
55groupIdorg.jboss.as.plugins/groupId
56artifactIdjboss-as-maven-plugin/artifactId
57version${version.jboss.maven.plugin}/version
58/plugin
59/plugins
60/build
61
62
63/project
ok,打完收工!