肇庆百度网站推广,页面设计排版网站,重庆专业网站推广费用,郑州 高端网站建设jax-ws cxf对于服务器核心Java开发人员来说#xff0c;向世界“展示”的唯一方法是使用API。 今天的帖子都是关于JAX-RS的 #xff1a;使用Java编写和公开RESTful服务。 但是#xff0c;我们不会使用涉及应用程序服务器#xff0c;WAR打包以及诸如此类的传统的重量级方… jax-ws cxf 对于服务器核心Java开发人员来说向世界“展示”的唯一方法是使用API。 今天的帖子都是关于JAX-RS的 使用Java编写和公开RESTful服务。 但是我们不会使用涉及应用程序服务器WAR打包以及诸如此类的传统的重量级方法来做到这一点。 相反我们将使用很棒的Apache CXF框架并且一如既往地依靠Spring将所有组件连接在一起。 当然我们也不会停止因为我们需要一个Web服务器来运行我们的服务。 使用胖子或一个罐子的概念我们将Jetty服务器嵌入到我们的应用程序中并使最终的JAR可重新分发包括所有依赖项并且可运行。 这是很多工作所以让我们开始吧。 如前所述我们将使用Apache CXF Spring和Jetty作为构建块因此让我们在POM文件中对其进行描述。 值得一提的另一个依赖项是出色的用于JSON处理的Jackson库。 project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemalocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelversion4.0.0/modelversiongroupidcom.example/groupidartifactidspring-one-jar/artifactidversion0.0.1-SNAPSHOT/versionpackagingjar/packagingpropertiesproject.build.sourceencodingUTF-8/project.build.sourceencodingorg.apache.cxf.version2.7.2/org.apache.cxf.versionorg.springframework.version3.2.0.RELEASE/org.springframework.versionorg.eclipse.jetty.version8.1.8.v20121106/org.eclipse.jetty.version/propertiesdependencies dependencygroupidorg.apache.cxf/groupidartifactidcxf-rt-frontend-jaxrs/artifactidversion${org.apache.cxf.version}/version/dependencydependencygroupidjavax.inject/groupidartifactidjavax.inject/artifactidversion1/version/dependencydependencygroupidorg.codehaus.jackson/groupidartifactidjackson-jaxrs/artifactidversion1.9.11/version/dependencydependencygroupidorg.codehaus.jackson/groupidartifactidjackson-mapper-asl/artifactidversion1.9.11/version/dependencydependencygroupidcglib/groupidartifactidcglib-nodep/artifactidversion2.2/version/dependencydependencygroupidorg.springframework/groupidartifactidspring-core/artifactidversion${org.springframework.version}/version/dependencydependencygroupidorg.springframework/groupidartifactidspring-context/artifactidversion${org.springframework.version}/version/dependencydependencygroupidorg.springframework/groupidartifactidspring-web/artifactidversion${org.springframework.version}/version/dependencydependencygroupidorg.eclipse.jetty/groupidartifactidjetty-server/artifactidversion${org.eclipse.jetty.version}/version/dependencydependencygroupidorg.eclipse.jetty/groupidartifactidjetty-webapp/artifactidversion${org.eclipse.jetty.version/version/dependency /dependenciesbuildpluginsplugingroupidorg.apache.maven.plugins/groupidartifactidmaven-compiler-plugin/artifactidversion3.0/versionconfigurationsource1.6/sourcetarget1.6/target/configuration/plugin plugingroupidorg.apache.maven.plugins/groupidartifactidmaven-jar-plugin/artifactidconfigurationarchivemanifestmainclasscom.example.Starter/mainclass/manifest/archive/configuration/pluginplugingroupidorg.dstovall/groupidartifactidonejar-maven-plugin/artifactidversion1.4.4/versionexecutionsexecutionconfigurationonejarversion0.97/onejarversionclassifieronejar/classifier/configurationgoalsgoalone-jar/goal/goals/execution/executions/plugin/plugins/buildpluginrepositoriespluginrepositoryidonejar-maven-plugin.googlecode.com/idurlhttp://onejar-maven-plugin.googlecode.com/svn/mavenrepo/url/pluginrepository/pluginrepositoriesrepositoriesrepositoryidmaven2-repository.dev.java.net/idurlhttp://download.java.net/maven/2//url/repository/repositories
/project 它有很多东西但是应该很清楚。 现在我们准备从简单的JAX-RS应用程序开始开发第一个JAX-RS服务。 package com.example.rs;import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;ApplicationPath( api )
public class JaxRsApiApplication extends Application {
} 看起来很简单我们的应用程序将/ api定义为JAX-RS服务的入口路径。 该样本服务将管理由Person类代表的人员 。 package com.example.model;public class Person {private String email;private String firstName;private String lastName;public Person() {}public Person( final String email ) {this.email email;}public String getEmail() {return email;}public void setEmail( final String email ) {this.email email;}public String getFirstName() {return firstName;}public String getLastName() {return lastName;}public void setFirstName( final String firstName ) {this.firstName firstName;}public void setLastName( final String lastName ) {this.lastName lastName;}
} 并遵循基本的业务服务为简单起见不包括数据库或任何其他存储。 package com.example.services;import java.util.ArrayList;
import java.util.Collection;import org.springframework.stereotype.Service;import com.example.model.Person;Service
public class PeopleService {public Collection Person getPeople( int page, int pageSize ) {Collection Person persons new ArrayList Person ( pageSize );for( int index 0; index pageSize; index ) {persons.add( new Person( String.format( person%dat.com, ( pageSize * ( page - 1 ) index 1 ) ) ) );}return persons;}public Person addPerson( String email ) {return new Person( email );}
} 如您所见我们将根据请求的页面动态生成人员列表。 标准的Spring注释Service将此类标记为服务bean。 我们的JAX-RS服务PeopleRestService将使用它来检索人员如以下代码所示。 package com.example.rs;import java.util.Collection;import javax.inject.Inject;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;import com.example.model.Person;
import com.example.services.PeopleService;Path( /people )
public class PeopleRestService {Inject private PeopleService peopleService;Produces( { application/json } )GETpublic Collection Person getPeople( QueryParam( page) DefaultValue( 1 ) final int page ) {return peopleService.getPeople( page, 5 );}Produces( { application/json } )PUTpublic Person addPerson( FormParam( email ) final String email ) {return peopleService.addPerson( email );}
} 虽然简单但该类需要更多说明。 首先我们要向/ people端点公开我们的RESTful服务。 将其与/ api 我们的JAX-RS应用程序所在的位置组合则以/ api / people作为限定路径给出。 现在只要有人向该路径发出HTTP GET 就应该调用方法getPeople 。 此方法接受可选的参数页面 默认值为1并以JSON形式返回人员列表。 反过来如果有人向同一路径发出HTTP PUT 则应调用addPerson方法使用必需的参数email 并将new person作为JSON返回。 现在让我们看一下应用程序的核心Spring配置。 package com.example.config;import java.util.Arrays;import javax.ws.rs.ext.RuntimeDelegate;import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import com.example.rs.JaxRsApiApplication;
import com.example.rs.PeopleRestService;
import com.example.services.PeopleService;Configuration
public class AppConfig { Bean( destroyMethod shutdown )public SpringBus cxf() {return new SpringBus();}Beanpublic Server jaxRsServer() {JAXRSServerFactoryBean factory RuntimeDelegate.getInstance().createEndpoint( jaxRsApiApplication(), JAXRSServerFactoryBean.class );factory.setServiceBeans( Arrays. Object asList( peopleRestService() ) );factory.setAddress( / factory.getAddress() );factory.setProviders( Arrays. Object asList( jsonProvider() ) );return factory.create();}Bean public JaxRsApiApplication jaxRsApiApplication() {return new JaxRsApiApplication();}Bean public PeopleRestService peopleRestService() {return new PeopleRestService();}Bean public PeopleService peopleService() {return new PeopleService();}Beanpublic JacksonJsonProvider jsonProvider() {return new JacksonJsonProvider();}
} 它看起来并不复杂但是在后台却发生了很多事情。 让我们将其分解为香料。 这里的两个关键组件是工厂JAXRSServerFactoryBean 它为配置我们的JAX-RS服务器实例提供了所有繁重的工作而SpringBus实例则将 Spring和Apache CXF无缝地粘合在一起。 所有其他组件代表常规的Spring Bean。 图片上还没有嵌入Jetty Web服务器实例。 我们的主要应用程序类Starter正是这样做的。 package com.example;import org.apache.cxf.transport.servlet.CXFServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;import com.example.config.AppConfig;public class Starter {public static void main( final String[] args ) throws Exception {Server server new Server( 8080 );// Register and map the dispatcher servletfinal ServletHolder servletHolder new ServletHolder( new CXFServlet() );final ServletContextHandler context new ServletContextHandler(); context.setContextPath( / );context.addServlet( servletHolder, /rest/* ); context.addEventListener( new ContextLoaderListener() );context.setInitParameter( contextClass, AnnotationConfigWebApplicationContext.class.getName() );context.setInitParameter( contextConfigLocation, AppConfig.class.getName() );server.setHandler( context );server.start();server.join(); }
} 浏览此代码后我们发现我们正在端口8080上运行Jetty服务器实例我们正在配置Apache CXF servlet来处理/ rest / *路径上的所有请求连同我们的JAX-RS应用程序和服务一起为我们提供了/ rest / api / people 我们使用上面定义的配置添加了参数化的Spring上下文侦听器最后启动了服务器。 此时我们拥有的是托管我们的JAX-RS服务的功能完善的Web服务器。 让我们来看看它的作用。 首先让我们将其包装为单个可运行和可再分配的脂肪或一个罐子 mvn clean package 让我们从目标文件夹中拾取位并运行它们 java -jar target/spring-one-jar-0.0.1-SNAPSHOT.one-jar.jar 我们应该看到这样的输出 2013-01-19 11:43:08.636:INFO:oejs.Server:jetty-8.1.8.v20121106
2013-01-19 11:43:08.698:INFO:/:Initializing Spring root WebApplicationContext
Jan 19, 2013 11:43:08 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Jan 19, 2013 11:43:08 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Sat Jan 19 11:43:08 EST 2013]; root of context hierarchy
Jan 19, 2013 11:43:08 AM org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider registerDefaultFilters
INFO: JSR-330 javax.inject.Named annotation found and supported for component scanning
Jan 19, 2013 11:43:08 AM org.springframework.web.context.support.AnnotationConfigWebApplicationContext loadBeanDefinitions
INFO: Successfully resolved class for [com.example.config.AppConfig]
Jan 19, 2013 11:43:09 AM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
INFO: JSR-330 javax.inject.Inject annotation found and supported for autowiring
Jan 19, 2013 11:43:09 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory1f8166e5:
defining beans [org.springframework.context.annotation.internal
ConfigurationAnnotationProcessor,
org.springframework.context.annotation.internalAutowiredAnnotationProcessor,
org.springframework.context.annotation.internalRequiredAnnotationProces
sor,org.springframework.context.annotation.internalCommonAnnotationProcessor,appConfig,
org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,c
xf,jaxRsServer,jaxRsApiApplication,peopleRestService,peopleService,jsonProvider]; root of factory hierarchy
Jan 19, 2013 11:43:10 AM org.apache.cxf.endpoint.ServerImpl initDestination
INFO: Setting the servers publish address to be /api
Jan 19, 2013 11:43:10 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 2227 ms
2013-01-19 11:43:10.957:INFO:oejsh.ContextHandler:started o.e.j.s.ServletContextHandler{/,null}
2013-01-19 11:43:11.019:INFO:oejs.AbstractConnector:Started SelectChannelConnector0.0.0.0:8080 让我们的服务器启动并运行让我们向它发出一些HTTP请求以确保一切都按预期运行 curl http://localhost:8080/rest/api/people?page2
[{email:person6at.com,firstName:null,lastName:null},{email:person7at.com,firstName:null,lastName:null},{email:person8at.com,firstName:null,lastName:null}, {email:person9at.com,firstName:null,lastName:null}, {email:person10at.com,firstName:null,lastName:null}
] curl http://localhost:8080/rest/api/people -X PUT -d emailab.com
{email:ab.com,firstName:null,lastName:null} 太棒了 并且请注意我们完全不使用XML 源代码 https : //github.com/reta/spring-one-jar/tree/jetty-embedded 在结束这篇文章之前我想提到一个很棒的项目Dropwizard 它使用了非常相似的概念但是由于Yammer的支持所以将其推向了出色的精心设计的框架。 参考 走向REST在Andriy Redko {devmind}博客上从我们的JCG合作伙伴 Andrey Redko 嵌入Jetty与Spring和JAX-RSApache CXF 。 翻译自: https://www.javacodegeeks.com/2013/01/going-rest-embedding-jetty-with-spring-and-jax-rs-apache-cxf.htmljax-ws cxf