当前位置: 首页 > news >正文

手机移动端网站建设邢台网站建设哪家专业

手机移动端网站建设,邢台网站建设哪家专业,平面设计排版,用mvc做网站报告1引言 Spring 4为MVC应用程序带来了一些改进 。 在这篇文章中#xff0c;我将重点介绍宁静的Web服务#xff0c;并通过采用Spring 3.2实现的项目并将其升级到Spring 4来尝试这些改进。以下几点总结了本文的内容#xff1a; 从Spring 3.2迁移到Spring 4.0 变化中的Response… 1引言 Spring 4为MVC应用程序带来了一些改进 。 在这篇文章中我将重点介绍宁静的Web服务并通过采用Spring 3.2实现的项目并将其升级到Spring 4来尝试这些改进。以下几点总结了本文的内容 从Spring 3.2迁移到Spring 4.0 变化中的ResponseBody和包容的RestController 同步和异步调用 以下项目的源代码可以在github上找到 原始项目Spring3.2 迁移到Spring 4 2 Spring 3.2 RESTful示例 起始项目使用Spring 3.2 pom.xml 实现。 它包含一个Spring MVC应用程序该应用程序访问数据库以检索有关电视连续剧的数据。 让我们看一下其REST API以使其更加清晰 弹簧配置 import resourcedb-context.xml/!-- Detects annotations like Component, Service, Controller, Repository, Configuration -- context:component-scan base-packagexpadro.spring.web.controller,xpadro.spring.web.service/!-- Detects MVC annotations like RequestMapping -- mvc:annotation-driven/ db-context.xml !-- Registers a mongo instance -- bean idmongo classorg.springframework.data.mongodb.core.MongoFactoryBeanproperty namehost valuelocalhost / /beanbean idmongoTemplate classorg.springframework.data.mongodb.core.MongoTemplateconstructor-arg namemongo refmongo /constructor-arg namedatabaseName valuerest-db / /bean 服务实施 此类负责从mongoDB数据库中检索数据 Service public class SeriesServiceImpl implements SeriesService {Autowiredprivate MongoOperations mongoOps;Overridepublic Series[] getAllSeries() {ListSeries seriesList mongoOps.findAll(Series.class);return seriesList.toArray(new Series[0]);}Overridepublic Series getSeries(long id) {return mongoOps.findById(id, Series.class);}Overridepublic void insertSeries(Series series) {mongoOps.insert(series);}Overridepublic void deleteSeries(long id) {Query query new Query();Criteria criteria new Criteria(_id).is(id);query.addCriteria(criteria);mongoOps.remove(query, Series.class);} } 控制器实施 该控制器将处理请求并与服务进行交互以检索系列数据 Controller RequestMapping(value/series) public class SeriesController {private SeriesService seriesService;Autowiredpublic SeriesController(SeriesService seriesService) {this.seriesService seriesService;}RequestMapping(methodRequestMethod.GET)ResponseBodypublic Series[] getAllSeries() {return seriesService.getAllSeries();}RequestMapping(value/{seriesId}, methodRequestMethod.GET)public ResponseEntitySeries getSeries(PathVariable(seriesId) long id) {Series series seriesService.getSeries(id);if (series null) {return new ResponseEntitySeries(HttpStatus.NOT_FOUND);}return new ResponseEntitySeries(series, HttpStatus.OK);}RequestMapping(methodRequestMethod.POST)ResponseStatus(HttpStatus.CREATED)public void insertSeries(RequestBody Series series, HttpServletRequest request, HttpServletResponse response) {seriesService.insertSeries(series);response.setHeader(Location, request.getRequestURL().append(/).append(series.getId()).toString());}RequestMapping(value/{seriesId}, methodRequestMethod.DELETE)ResponseStatus(HttpStatus.NO_CONTENT)public void deleteSeries(PathVariable(seriesId) long id) {seriesService.deleteSeries(id);} } 整合测试 这些集成测试将在模拟Spring MVC环境中测试我们的控制器。 这样我们将能够测试处理程序方法的映射。 为此 MockMvc类变得非常有用。 如果您想学习如何编写Spring MVC控制器的测试我强烈推荐Petri Kainulainen编写的Spring MVC Test Tutorial系列。 RunWith(SpringJUnit4ClassRunner.class) WebAppConfiguration ContextConfiguration(locations{classpath:xpadro/spring/web/test/configuration/test-root-context.xml,classpath:xpadro/spring/web/configuration/app-context.xml}) public class SeriesIntegrationTest {private static final String BASE_URI /series;private MockMvc mockMvc;Autowiredprivate WebApplicationContext webApplicationContext;Autowiredprivate SeriesService seriesService;Beforepublic void setUp() {reset(seriesService);mockMvc MockMvcBuilders.webAppContextSetup(webApplicationContext).build();when(seriesService.getAllSeries()).thenReturn(new Series[]{new Series(1, The walking dead, USA, Thriller), new Series(2, Homeland, USA, Drama)});when(seriesService.getSeries(1L)).thenReturn(new Series(1, Fringe, USA, Thriller));}Testpublic void getAllSeries() throws Exception {mockMvc.perform(get(BASE_URI).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentType(application/json;charsetUTF-8)).andExpect(jsonPath($, hasSize(2))).andExpect(jsonPath($[0].id, is(1))).andExpect(jsonPath($[0].name, is(The walking dead))).andExpect(jsonPath($[0].country, is(USA))).andExpect(jsonPath($[0].genre, is(Thriller))).andExpect(jsonPath($[1].id, is(2))).andExpect(jsonPath($[1].name, is(Homeland))).andExpect(jsonPath($[1].country, is(USA))).andExpect(jsonPath($[1].genre, is(Drama)));verify(seriesService, times(1)).getAllSeries();verifyZeroInteractions(seriesService);}Testpublic void getJsonSeries() throws Exception {mockMvc.perform(get(BASE_URI /{seriesId}, 1L).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentType(application/json;charsetUTF-8)).andExpect(jsonPath($.id, is(1))).andExpect(jsonPath($.name, is(Fringe))).andExpect(jsonPath($.country, is(USA))).andExpect(jsonPath($.genre, is(Thriller)));verify(seriesService, times(1)).getSeries(1L);verifyZeroInteractions(seriesService);}Testpublic void getXmlSeries() throws Exception {mockMvc.perform(get(BASE_URI /{seriesId}, 1L).accept(MediaType.APPLICATION_XML)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_XML)).andExpect(xpath(/series/id).string(1)).andExpect(xpath(/series/name).string(Fringe)).andExpect(xpath(/series/country).string(USA)).andExpect(xpath(/series/genre).string(Thriller));verify(seriesService, times(1)).getSeries(1L);verifyZeroInteractions(seriesService);} } 我正在展示一些已实施的测试。 检查SeriesIntegrationTesting以获得完整的实现。 功能测试 该应用程序使用RestTemplate类包含一些功能测试。 您需要部署Web应用程序才能对此进行测试。 RunWith(SpringJUnit4ClassRunner.class) ContextConfiguration(locations{classpath:xpadro/spring/web/configuration/root-context.xml,classpath:xpadro/spring/web/configuration/app-context.xml}) public class SeriesFunctionalTesting {private static final String BASE_URI http://localhost:8080/spring-rest-api-v32/spring/series;private RestTemplate restTemplate new RestTemplate();Autowiredprivate MongoOperations mongoOps;Beforepublic void setup() {ListHttpMessageConverter? converters new ArrayListHttpMessageConverter?();converters.add(new StringHttpMessageConverter());converters.add(new Jaxb2RootElementHttpMessageConverter());converters.add(new MappingJacksonHttpMessageConverter());restTemplate.setMessageConverters(converters);initializeDatabase();}private void initializeDatabase() {try {mongoOps.dropCollection(series);mongoOps.insert(new Series(1, The walking dead, USA, Thriller));mongoOps.insert(new Series(2, Homeland, USA, Drama));} catch (DataAccessResourceFailureException e) {fail(MongoDB instance is not running);}}Testpublic void getAllSeries() {Series[] series restTemplate.getForObject(BASE_URI, Series[].class);assertNotNull(series);assertEquals(2, series.length);assertEquals(1L, series[0].getId());assertEquals(The walking dead, series[0].getName());assertEquals(USA, series[0].getCountry());assertEquals(Thriller, series[0].getGenre());assertEquals(2L, series[1].getId());assertEquals(Homeland, series[1].getName());assertEquals(USA, series[1].getCountry());assertEquals(Drama, series[1].getGenre());}Testpublic void getJsonSeries() {ListHttpMessageConverter? converters new ArrayListHttpMessageConverter?();converters.add(new MappingJacksonHttpMessageConverter());restTemplate.setMessageConverters(converters);String uri BASE_URI /{seriesId};ResponseEntitySeries seriesEntity restTemplate.getForEntity(uri, Series.class, 1l);assertNotNull(seriesEntity.getBody());assertEquals(1l, seriesEntity.getBody().getId());assertEquals(The walking dead, seriesEntity.getBody().getName());assertEquals(USA, seriesEntity.getBody().getCountry());assertEquals(Thriller, seriesEntity.getBody().getGenre());assertEquals(MediaType.parseMediaType(application/json;charsetUTF-8), seriesEntity.getHeaders().getContentType());}Testpublic void getXmlSeries() {String uri BASE_URI /{seriesId};ResponseEntitySeries seriesEntity restTemplate.getForEntity(uri, Series.class, 1L);assertNotNull(seriesEntity.getBody());assertEquals(1l, seriesEntity.getBody().getId());assertEquals(The walking dead, seriesEntity.getBody().getName());assertEquals(USA, seriesEntity.getBody().getCountry());assertEquals(Thriller, seriesEntity.getBody().getGenre());assertEquals(MediaType.APPLICATION_XML, seriesEntity.getHeaders().getContentType());} } 就是这样Web应用程序已经过测试并正在运行。 现在是时候迁移到Spring 4了。 3迁移到Spring 4 检查此页面以阅读有关从早期版本的Spring框架进行迁移的信息 3.1更改Maven依赖项 本节说明应修改哪些依赖项。 您可以在此处查看完整的pom.xml。 第一步是将Spring依赖版本从3.2.3.RELEASE更改为4.0.0.RELEASE dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion4.0.0.RELEASE/version /dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactIdversion4.0.0.RELEASE/version /dependency 下一步是更新到Servlet 3.0规范。 这一步很重要因为某些Spring功能基于Servlet 3.0因此将不可用。 事实上试图执行SeriesIntegrationTesting将导致一个ClassNotFoundException由于这个原因这也解释了这里 。 dependencygroupIdjavax.servlet/groupIdartifactIdjavax.servlet-api/artifactIdversion3.1.0/version /dependency3.2更新Spring名称空间 不要忘记更改spring配置文件的名称空间 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 请查看第2节中链接的信息页面因为有关mvc名称空间的某些更改。 3.3弃用杰克逊库 如果再次检查SeriesFunctionalTesting设置方法您会发现杰克逊转换器已被弃用。 如果您尝试运行测试由于Jackson库中的方法更改它将抛出NoSuchMethodError java.lang.NoSuchMethodError: org.codehaus.jackson.map.ObjectMapper.getTypeFactory()Lorg/codehaus/jackson/map/type/TypeFactory 在Spring4中已不再支持Jackson 1.x而支持Jackson v2。 让我们更改旧的依赖项 dependencygroupIdorg.codehaus.jackson/groupIdartifactIdjackson-mapper-asl/artifactIdversion1.4.2/version /dependency 对于这些 dependencygroupIdcom.fasterxml.jackson.core/groupIdartifactIdjackson-core/artifactIdversion2.3.0/version /dependencydependencygroupIdcom.fasterxml.jackson.core/groupIdartifactIdjackson-databind/artifactIdversion2.3.0/version /dependency 最后如果要显式注册消息转换器则需要为新版本更改不推荐使用的类 //converters.add(new MappingJacksonHttpMessageConverter()); converters.add(new MappingJackson2HttpMessageConverter());3.4迁移完成 迁移完成。 现在您可以运行该应用程序并执行其测试。 下一节将回顾我在本文开头提到的一些改进。 4 Spring 4 Web改进 4.1 ResponseBody和RestController 如果您的REST API以JSON或XML格式提供内容则某些API方法用RequestMapping注释的返回类型将用ResponseBody注释。 使用此注释返回类型将包含在响应主体中。 在Spring4中我们可以通过两种方式简化此过程 用ResponseBody注释控制器 现在可以在类型级别添加此注释。 这样注释就被继承了我们不必强迫将注释放在每个方法中。 Controller ResponseBody public class SeriesController { 用RestController注释控制器 RestController public class SeriesController { 此注释甚至进一步简化了控制器。 如果我们检查此注释我们将看到它本身已使用Controller和ResponseBody进行了注释 Target(ElementType.TYPE) Retention(RetentionPolicy.RUNTIME) Documented Controller ResponseBody public interface RestController { 包括此注释将不会影响使用ResponseEntity注释的方法。 处理程序适配器查找返回值处理程序的列表以确定谁有能力处理响应。 的 在ResponseBody类型之前会先询问负责处理ResponseEntity返回类型的处理程序 因此如果方法中存在ResponseEntity批注则将使用该处理程序。 4.2异步调用 使用实用程序类RestTemplate调用RESTful服务将阻塞线程直到它收到响应为止。 Spring 4包含AsyncRestTemplate以便执行异步调用。 现在您可以拨打电话继续进行其他计算并在以后检索响应。 Test public void getAllSeriesAsync() throws InterruptedException, ExecutionException {logger.info(Calling async /series);FutureResponseEntitySeries[] futureEntity asyncRestTemplate.getForEntity(BASE_URI, Series[].class);logger.info(Doing other async stuff...);logger.info(Blocking to receive response...);ResponseEntitySeries[] entity futureEntity.get();logger.info(Response received);Series[] series entity.getBody();assertNotNull(series);assertEquals(2, series.length);assertEquals(1L, series[0].getId());assertEquals(The walking dead, series[0].getName());assertEquals(USA, series[0].getCountry());assertEquals(Thriller, series[0].getGenre());assertEquals(2L, series[1].getId());assertEquals(Homeland, series[1].getName());assertEquals(USA, series[1].getCountry());assertEquals(Drama, series[1].getGenre()); } 带回调的异步调用 尽管前面的示例进行了异步调用但如果尚未发送响应则尝试使用futureEntity.get检索响应时线程将阻塞。 AsyncRestTemplate返回ListenableFuture 它扩展了Future并允许我们注册一个回调。 以下示例进行异步调用并继续执行其自己的任务。 当服务返回响应时它将由回调处理 Test public void getAllSeriesAsyncCallable() throws InterruptedException, ExecutionException {logger.info(Calling async callable /series);ListenableFutureResponseEntitySeries[] futureEntity asyncRestTemplate.getForEntity(BASE_URI, Series[].class);futureEntity.addCallback(new ListenableFutureCallbackResponseEntitySeries[]() {Overridepublic void onSuccess(ResponseEntitySeries[] entity) {logger.info(Response received (async callable));Series[] series entity.getBody();validateList(series);}Overridepublic void onFailure(Throwable t) {fail();}});logger.info(Doing other async callable stuff ...);Thread.sleep(6000); //waits for the service to send the response }5结论 我们使用了Spring 3.2.x Web应用程序并将其迁移到Spring 4.0.0的新版本中。 我们还回顾了可以应用于Spring 4 Web应用程序的一些改进。 我正在Google Plus和Twitter上发布我的新帖子。 如果您要更新新内容请关注我。 参考在XavierPadró的Blog博客上从我们的JCG合作伙伴 Xavier Padro将Spring MVC RESTful Web服务迁移到Spring 4 。 翻译自: https://www.javacodegeeks.com/2014/02/migrating-spring-mvc-restful-web-services-to-spring-4.html
http://www.yutouwan.com/news/130301/

相关文章:

  • 网站建设后的团队总结wordpress 申请
  • 网站用户体验设计传奇类游戏网站
  • 西宁整站优化电商网站建设收费
  • asp网站开发教程入门上海网站制作公司
  • 实木餐桌椅移动网站建设山西省诚信建设网站
  • 蚂蚁中国网站建设建筑工程包括哪些项目
  • 科技网站建设分析网站建设如何交税
  • 网站首页 关键词兰州建设局网站公告
  • 做公司的网站的需求有哪些内容前端开发常用网站
  • 广州市建设工程招标管理办公室网站高端建筑材料有哪些
  • 网站备案平台的服务简介做的好的电商网站项目
  • 网站建设dw实训总结陕西网站建设设计
  • 无代码快速搭建网站网页制作代码html制作一个网页
  • 网站整体设计流程国际人才网招聘网
  • tp5.1做的网站云南个旧建设局网站
  • 团购网站自个做国内国际新闻最新消息10条
  • 汉阴网站建设电商到底是什么
  • 北京网站建设项目腾讯云服务器12元一年
  • 营销策略从哪几个方面分析seo内容优化方法
  • 一般去哪个网站做写手wordpress图片尺寸
  • 怎样网站制作设计网站备案信息核验单怎么
  • 公司网站建设为什么不直接买模版网站信息管理平台
  • 那些网站可以做宣传环境设计公司排名
  • 用模板网站做h5宣传页多少钱win7iis如何做网站
  • 城阳区城市规划建设局网站信息技术的网站建设是什么
  • 2345网址导航官方网站互联网门户网站是什么
  • 站长之家官网网址上海比较好的装修公司
  • 网站建设自主建设美术对网站开发有用吗
  • 网站建设业务员招聘学院宣传网站制作
  • 沃航科技网站开发青海公路建设服务网站