搭建 Spring MVC 接口环境

 1、maven构建项目,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.loong</groupId>
     <artifactId>loong</artifactId>
     <version>1.0-SNAPSHOT</version>
     <packaging>war</packaging>
     <name>loong</name>
     <url>http://maven.apache.org</url>
     <!-- 获取架包的版本 -->
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <spring.version>3.2.5.RELEASE</spring.version>
         <log4j.version>1.2.17</log4j.version>
         <jackson.version>1.9.13</jackson.version>
     </properties>
     <dependencies>
         <!-- Spring MVC使用的架包 -->
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-web</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-webmvc</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <!-- 日志 -->
         <dependency>
             <groupId>log4j</groupId>
             <artifactId>log4j</artifactId>
             <version>${log4j.version}</version>
         </dependency>
         <!-- Json解析 -->
         <dependency>
             <groupId>org.codehaus.jackson</groupId>
             <artifactId>jackson-mapper-asl</artifactId>
             <version>${jackson.version}</version>
         </dependency>
     </dependencies>
 </project>

 

 2、配置web.xml(webapp/WEB-INF/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"
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
         
     <display-name>loong</display-name>
     <welcome-file-list>
         <welcome-file>index.html</welcome-file>
     </welcome-file-list>
     
     <!-- Spring MVN 配置 -->
     <servlet>
         <servlet-name>mvc</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <init-param>
            <param-name>contextConfigLocation</param-name>
             <param-value>classpath:context/app.xml</param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>
     </servlet>
     <servlet-mapping>
         <servlet-name>mvc</servlet-name>
         <url-pattern>*.json</url-pattern>
     </servlet-mapping>
     
     <!-- 编码处理 -->
     <filter>
         <filter-name>encoding</filter-name>
         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
         <init-param>
             <param-name>encoding</param-name>
             <param-value>UTF-8</param-value>
         </init-param>
     </filter>
     <filter-mapping>
         <filter-name>encoding</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>
     
     <!-- 日志 -->
     <listener>
         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
     </listener>
     <context-param>
         <param-name>log4jConfigLocation</param-name>
         <param-value>classpath:properties/log4j.properties</param-value>
     </context-param>
 </web-app>

 

3、Spring配置(resources/context/app.xml)

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://www.springframework.org/schema/beans"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
   
     <!-- 扫描注释 -->
     <context:component-scan base-package="com.loong" />
     <!-- 开启mvn -->
     <mvc:annotation-driven />
     <!-- mvc配置 -->
     <!-- <import resource="app-mvc.xml" /> -->
 </beans>

 

 4、log4j日志配置(resources/properties/log4j.properties)

 log4j.rootLogger=info,C,I,E
 
 #Console appender
 log4j.appender.C.Threshold=info
 log4j.appender.C=org.apache.log4j.ConsoleAppender
 log4j.appender.C.layout=org.apache.log4j.PatternLayout
 log4j.appender.C.layout.ConversionPattern=%m%n
 
 #InfoFile appender
 log4j.appender.I.Threshold=info
 log4j.appender.I=org.apache.log4j.DailyRollingFileAppender
 log4j.appender.I.file=${catalina.base}/logs/log.log
 log4j.appender.I.layout=org.apache.log4j.PatternLayout
 log4j.appender.I.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t][%C:%L(%M)] - %m%n

 #ErrorFile appender
 log4j.appender.E.Threshold=error
 log4j.appender.E=org.apache.log4j.DailyRollingFileAppender 
 log4j.appender.E.file=${catalina.base}/logs/error.log
 log4j.appender.E.layout=org.apache.log4j.PatternLayout
 log4j.appender.E.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t][%C:%L(%M)] - %m%n

 

 5、新建一个测试类

 @Controller
 @RequestMapping("demo")
 public class DemoResource {
     @RequestMapping("test")
     @ResponseBody
     public DemoResult test(DemoParam param) {
         DemoResult result = new DemoResult();
         result.setSucc(0);
         result.setData(param); 
         return result;
     }
 }

    访问参数JavaBean:

 public class DemoParam {
     private String param;
     public String getParam() {
         return param;
     }
     public void setParam(String param) {
         this.param = param;
     }
 }

    返回参数JavaBean:

 public class DemoResult {
     private Integer succ;
     private String message;
     private Object data;
     public Integer getSucc() {
         return succ;
     }
     public void setSucc(Integer succ) {
         this.succ = succ;
     }
     public String getMessage() {
         return message;
     }
     public void setMessage(String message) {
         this.message = message;
     }
     public Object getData() {
         return data;
     }
     public void setData(Object data) {
         this.data = data;
     }
 }

     启动项目,访问 localhost:8080/loong/demo/test.json?param=1,返回:{"succ":0,"message":null,"data":{"param":"1"}},测试成功。

     Spring MVC常用注释:
         @RequestMapping 映射地址
         @ResponseBody 声明返回对象作为响应正文
         @RequestBody 声明正文作为请求参数(一般访问参数是个复杂对象时会用到)
         @ControllerAdvice 声明是控制器
         @ExceptionHandler 异常拦截
         @InitBinder 请求初始化处理

     备忘下:Spring的类注释:@Controller(接口层)、@Service(服务层)、@Repository(持久层)、@Component(组件)

 6、去除JavaBean中字段返回

    修改一下返回的Bean:

 @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
 public class DemoResult {
     @JsonIgnore
     private Integer succ;
     private String message;
     private Object data;
     public Integer getSucc() {
         return succ;
     }
     public void setSucc(Integer succ) {
         this.succ = succ;
     }
     public String getMessage() {
         return message;
     }
     public void setMessage(String message) {
         this.message = message;
     }
     public Object getData() {
         return data;
     }
     public void setData(Object data) {
         this.data = data;
     }
 }

     再测试下访问 localhost:8080/loong/demo/test.json?param=1,返回:{"data":{"param":"1"}}。

     其中:
         @JsonSerialize 定义转换的过滤规则,JsonSerialize.Inclusion.NON_NULL 会过滤为空的对象。
         @JsonIgnore 被声明的变量会直接被过滤掉。

 7、增加异常处理

    新增一个异常处理类:

 /**
  * 接口异常处理器
  * 
  * @author 张成轩
  */
 @ControllerAdvice
 public class RestExceptionHandler {
     /**
      * 运行时异常处理
      * 
      * @param ex 异常对象
      * @return vo
      */
     @ExceptionHandler(RuntimeException.class)
     @ResponseBody
     public DemoResult runtimeExceptionHandler(RuntimeException ex) {
         DemoResult result = new DemoResult();
         result.setSucc(-1);
         result.setMessage(ex.getMessage());
         return result;
     }
     /**
      * 通用异常处理
      * 
      * @param ex 异常对象
      * @return vo
      */
     @ExceptionHandler(Exception.class)
     @ResponseBody
     public DemoResult exceptionHandler(Exception ex) {
         DemoResult result = new DemoResult();
         result.setSucc(-2);
         result.setMessage(ex.getMessage());
         return result;
     }
 }

     修改一下测试类:

 public DemoResult test(DemoParam param) {
     if(param.getParam() == null){
         throw new RuntimeException("异常:访问参数为空");
     }
     ......
 }

     测试一下,localhost:8080/loong/demo/test.json,返回:{"succ":-1,"message":"异常:访问参数为空","data":null}。


 8、增加访问初始化处理

     新增一个访问初始化类

 /**
  * 接口访问初始化处理器
  * 
  * @author 张成轩
  */
 @ControllerAdvice
 public class RestInitHandler {
     /**
      * 接口访问参数初始化
      * 
      * @param binder
      * @param request
      */
     @InitBinder
     public void beforeHandle(WebDataBinder binder, WebRequest request) {
     }
 }


9、新增Jsonp处理

     新增Jsonp处理类:

 /**
  * JsonP处理器
  * 
  * @author 张成轩
  */
 public class JsonpHandler extends MappingJacksonHttpMessageConverter {
     /** 回调参数 */
     private final static String CALLBACK = "callback";
     /** Jsonp起始 */
     private final static String START = "(";
     /** Jsonp终止 */
     private final static String END = "); ";
     @Override
     protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException,
             HttpMessageNotWritableException {
         JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
         // 获取Json构造对象
         JsonGenerator jsonGenerator = getObjectMapper().getJsonFactory().createJsonGenerator(
                 outputMessage.getBody(), encoding);
         try {
             HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                     .currentRequestAttributes()).getRequest();
             // 获取回调方法名
             String callback = request.getParameter(CALLBACK);
             if (callback == null || callback.trim().isEmpty()) {
                 // 正常方式
                 getObjectMapper().writeValue(jsonGenerator, o);
             } else {
                 // 写回调Js信息
                 jsonGenerator.writeRaw(callback);
                 jsonGenerator.writeRaw(START);
                 getObjectMapper().writeValue(jsonGenerator, o);
                 jsonGenerator.writeRaw(END);
             }
             jsonGenerator.flush();
         } catch (JsonProcessingException e) {
             throw new HttpMessageNotWritableException(e.getMessage(), e);
         }
     }
 }

     修改一下Spring配置文件,(resources/context/app.xml)

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://www.springframework.org/schema/beans"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
   
     <!-- 扫描注释 -->
     <context:component-scan base-package="com.loong" />
     <!-- 开启mvn -->
     <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
         <mvc:message-converters>
             <bean class="com.loong.handler.JsonpHandler" />
         </mvc:message-converters>
     </mvc:annotation-driven>
     <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
         <property name="favorPathExtension" value="true" />
         <property name="favorParameter" value="true" />
         <property name="ignoreAcceptHeader" value="true" />
         <property name="defaultContentType" value="application/json;charset=UTF-8" />
         <property name="parameterName" value="dataType" />
         <property name="mediaTypes">
             <map>
                 <entry key="json" value="application/json;charset=UTF-8" />
                 <entry key="jsonp" value="application/x-javascript;charset=UTF-8" />
                 <entry key="xml" value="application/xml;charset=UTF-8" />
                 <entry key="html" value="text/html;charset=UTF-8" />
             </map>
         </property>
     </bean>
</beans>

     maven里引入一下servlet架包

 <!-- servlet -->
 <dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>servlet-api</artifactId>
     <version>2.5</version>
     <scope>provided</scope>
 </dependency>

    测试下
         访问:localhost:8080/loong/demo/test.json?param=1&callback=test,返回:test({"succ":0,"message":null,"data":{"param":"1"}});。
         访问:localhost:8080/loong/demo/test.json?param=1,返回:{"succ":0,"message":null,"data":{"param":"1"}}。

你可能感兴趣的:(springMVC)