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" id="WebApp_ID" version="3.0">
<display-name>xyb-consensus</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/*.xml</param-value>
</context-param>
<listener>
<description>spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet>
<description>spring mvc servlet</description>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>spring mvc 配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 解决HTTP PUT请求Spring无法获取请求参数的问题 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<servlet-name>springMvc</servlet-name>
</filter-mapping>
<display-name>api</display-name>
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
spring-mvc.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 注解驱动 -->
<mvc:annotation-driven />
<!-- 扫描包 -->
<context:component-scan base-package="com.email.api" />
<!-- 用于页面跳转,根据请求的不同跳转到不同页面,如请求index.do则跳转到/WEB-INF/jsp/index.jsp -->
<bean id="findJsp"
class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.do">findJsp</prop><!-- 表示index.do转向index.jsp页面 -->
<prop key="first.do">findJsp</prop><!-- 表示first.do转向first.jsp页面 -->
</props>
</property>
</bean>
<!-- 视图解析 -->
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<!-- 返回的视图模型数据需要经过jstl来处理 -->
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 对静态资源文件的访问 不支持访问WEB-INF目录 -->
<mvc:default-servlet-handler />
<bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 输出对象转JSON支持 -->
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringConverter"/>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
</beans>
注意的事,由于版本问题json的转换类是MappingJackson2HttpMessageConverter ,而不是MappingJacksonHttpMessageConverter
页面逻辑
package com.email.api;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
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.ResponseBody;
import com.email.bean.Bill;
import com.email.bean.Result;
import com.email.login.QQLoginTransaction;
import com.email.login._163LoginTransaction;
import com.email.transaction.Transaction;
import com.email.utils.ThreeTuple;
/**
* 爬虫Restful api
* @author fansl
*/
@Controller
public class EmailCrawlerApi {
@RequestMapping(value = "/bill/{email}/{password}", method = RequestMethod.GET)
public @ResponseBody Result getBill(@PathVariable("email") String email,@PathVariable("password") String password) {
// logger.info("获取邮件:" + email + "的账单!");
Result result = new Result();
Transaction login = null;
if(email.contains("qq.com")){
login = new QQLoginTransaction(email,password);
}else if(email.contains("163.com")||email.contains("126.com")||email.contains("yeah.net")||email.contains("188.com")){
login = new _163LoginTransaction(email,password);
}else {
result.setReason("此邮箱抓取功能未实现");
result.setBills(null);
}
ThreeTuple<Boolean, String, List<Bill>> results = login.isSuccess();
result.setBills(results.getThird());
result.setReason(results.getSecond());
result.setResult(results.getFirst());
return result;
}
}
访问地址验证
http://localhost:8080/email-crawler/bill/参数1/参数2