我们我们经常在老式的项目开发过程中,遇到找java包的问题;甚至有时候一找一天就过去了。maven 是我们开发工程师的福音,它可以根据我们的配置自动的下载并加装到我们的工程中,并在发布的时候同时发布对应的Java包。这样大大提高了我们的工作效率,更有时间学习前沿的技术。
什么是maven?
maven是专用于进行项目的配置管理工作;用maven创建的项目中必须包括一个pom.xml文件,用于设置依赖关系、项目的基本配置(grouId,artifactId,version等),编译项目时用插件、环境配置、发布管理、私服库配置等。这样我们不用太多关心项目的配置方面的问题,而是更专注于具体业务环境的开发。我们可以快速搭建起开发框架,通过maven来管理项目配置。
什么是restful?
一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。其实说白了就是让我们开发的代码更少,通过很少代码实现复杂的功能。
在maven项目中,我们需要有一个pom.xml文件;我们一实际举个例子,先讲一下项目背景:此项目主要是实现restful风格的接口项目,用于其他模块调用使用。开发的思路:先配置一个pom.xml文件 然后在webapp/WEB-INFO目录下创建web.xml文件用spring这个框架 模拟get、form提交的post方式来进行数据的交互。大家可以按照自己实际的要求删减配置。
pom.xml 文件如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.seven.spring</groupId> <artifactId>springtest</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <!-- 定义一些属于变量,用于配置中使用,方面版本升级 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <contextpath>easydoctor</contextpath> <java.version>1.8</java.version> <json-path-assert.version>2.0.0</json-path-assert.version> <h2.version>1.4.184</h2.version> <maven-compiler-plugin.version>3.1</maven-compiler-plugin.version> <tomcat7-maven-plugin.version>2.0-beta-1</tomcat7-maven-plugin.version> <maven-project-info-reports-plugin.version>2.7</maven-project-info-reports-plugin.version> <maven-javadoc-plugin.version>2.9.1</maven-javadoc-plugin.version> <maven-jxr-plugin.version>2.3</maven-jxr-plugin.version> </properties> <build> <finalName>springtest</finalName> <resources> <resource> <directory>src/main/resources/</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven-compiler-plugin.version}</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>utf-8</encoding> </configuration> </plugin> </plugins> </build> <dependencies> <!-- 用于spring mvc 项目开发需要 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency> <!-- 用于json 序列化 和反序列话 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.4</version> </dependency> <!-- servlet采用常见的接受请求和生成响应的编程模型 ,由sun公司开发的 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> <!-- provided 只影响在编译 测试阶段使用,不会在发布、打包等阶段将包放入lib目录下,常用于我们发布的目标容器中已经存在包 --> </dependency> <!-- 用于在代码中自动生成set/get --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.4</version> </dependency> <!-- 用于处理json对象需要的包 --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.8.4</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.8.4</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.8.4</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.8.4</version> </dependency> </dependencies> </project>
我们知道spring 的web项目是需要在webapp/WEB-INFO 目录下创建一个web.xml文档用户进行相关web运行的设定(类过滤器,事务、dispatchseverlet 配置等)
web.xml文档如下
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- <listener> <listener-class>com.seven.PreContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:META-INF/spring/root/*.xml</param-value> </context-param>--> <servlet> <servlet-name>rest</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:rest-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>rest</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- <filter> <display-name>SignerFilter</display-name> <filter-name>SignerFilter</filter-name> <filter-class>com.seven.SignerFilter</filter-class> </filter> <filter-mapping> <filter-name>SignerFilter</filter-name> <url-pattern>/apiController</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping> <filter> <display-name>ApiConvertFilter</display-name> <filter-name>ApiConvertFilter</filter-name> <filter-class>com.seven.api</filter-class> </filter> <filter-mapping> <filter-name>ApiConvertFilter</filter-name> <url-pattern>/apiController</url-pattern> </filter-mapping>--> </web-app>
在web.xml的配置文件中,classpath*:rest-context.xml
作用是会加载这个配置运行,常在此配置中配置一些Bean,用于容器加载的时候自动创建对应的类对象(一般创建的对象都是singleton模式);
rest-context.xml 文件如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!-- 扫描包下的annotation标记 Controller Service restController等 --> <context:component-scan base-package="com.seven.spring" /> <!-- 定义一个bean对象 --> <bean id="userDetails" class="com.seven.spring.UserDetails"/> <!-- 自动配置项 --> <mvc:annotation-driven /> <context:annotation-config/> <!-- 内容管理器配置 --> <bean id="contentManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="favorPathExtension" value="true"/> <property name="ignoreAcceptHeader" value="true" /> <property name="defaultContentType" value="text/html" /> <property name="useJaf" value="false"/> <property name="mediaTypes"> <map> <entry key="json" value="application/json" /> <entry key="html" value="text/html" /> <entry key="xml" value="application/xml" /> </map> </property> </bean> <!-- jsp视图解析器配置 主要设定ModelAndView 的前缀、后缀 --> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
基本配置完成后就是写代码了,我这边主要做一些简单的接口测试;相关代码如下
package com.seven.spring; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; @RestController public class SpringRestControllerDemo { @Autowired UserDetails userDetails; /* @RequestMapping(value="/springcontent", method=RequestMethod.GET,produces={"application/xml", "application/json"}) @RequestMapping(value="/springcontent.json", method=RequestMethod.GET) */ @RequestMapping(value="/springcontent",method=RequestMethod.POST) @ResponseStatus(HttpStatus.OK) public UserDetails getUser() { UserDetails userDetails = new UserDetails(); userDetails.setUserName("seven"); userDetails.setEmailId("[email protected]"); return userDetails; } @RequestMapping(value = "springcontentadd", method = RequestMethod.POST) public UserDetails addUser(@RequestBody UserDetails userDetails) { System.out.println("userdetail="+userDetails); return userDetails; } @RequestMapping(value = "springcontentpar", method = RequestMethod.POST) public String getRquerypar(@RequestParam("name") String userDetails) { System.out.println("userdetail="+userDetails); return userDetails; } @RequestMapping(value="/springcontent.htm", method=RequestMethod.GET) @ResponseStatus(HttpStatus.OK) public String getUserHtml() { //Test HTML view return "example"; } }
大家在自己的本机创建试试。
版权声明:本文为博主原创文章,未经博主允许不得转载。