去实习公司,组长让搭个spring mvc 框架接受url返回json格式的数据;
1 下载spring-framework-4.2.5.RELEASE 的文件,然后将其中lib放入工程中;
2 spring - servlet.xml配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="false">
<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<!-- 这里顺序不能反,一定先写text/html,不然ie下出现下载提示 -->
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
<context:component-scan base-package="com.mvc.rest" />
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp" />
</beans>
3 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"
id="WebApp_ID"
version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 应用上下文配置文件 -->
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring核心servlet -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- url-pattern配置为/,不带文件后缀,会造成其它静态文件(js,css等)不能访问。如配为*.do,则不影响静态文件的访问 -->
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
<!-- 配置spring核心servlet -->
用来拦截请求
4 测试code
@Controller
@RequestMapping(value = "/oversea_sarrs")
public class ChannelController {
@Autowired
private final static Logger log = LoggerFactory.getLogger(ChannelController.class);
/*
*例子表明是可以直接访问url的:http://localhost:8080/springmvc/oversea_sarrs/test
*return json 格式 数据
*/
@RequestMapping(value = "/test")
public @ResponseBody JSONObject test(HttpServletResponse response,HttpServletRequest request) throws ServletException, IOException {
Map map = new HashMap();
map.put("name", "json");
map.put("bool", Boolean.TRUE);
map.put("int", new Integer(1));
map.put("arr", new String[] { "a", "b" });
map.put("func", "function(i){ return this.arr[i]; }");
JSONObject json = JSONObject.fromObject(map);
return json;
}
}
返回 :<span style="font-family: Simsun;font-size:14px;">{"arr":["a","b"],"bool":true,"func":"function(i){ return this.arr[i]; }","int":1,"name":"json"}</span>
在这里说一下知识点的补充:
1 一开始是遇到总是报 receptedable represented exception ,就是不能返回json类型的格式 ,后来在spring-servlet.xml 中配置成功后,就不会报错了;
参考:http://my.oschina.net/haopeng/blog/324934
2 然后就是类型的格式可以转化为json
参考:http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/23/3096001.html
真心不错,之前我的配置结果总是显示返回的是一个null,后来看到可以转化为json的几种格式;
3 接下来又遇到一个
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderL,spring获取context的异常
最后发现是因为jar包并未真正加入项目中的结果,不知道这个是不是myecplise的坑;
网上解释:
1 出现这个问题的原因有可能的其中一点就是spring的jar包没有被加载,有的时候没有拷spring的jar包会出现这个错误,但是当你拷了以后这个错误并没有消失的时候就说明jar包没有被加载,切记spring的jar包一定要放在工程的lib下这样才能避免这个错误的发生。
解决办法:
参考:http://www.csdn123.com/html/topnews201408/77/6177.htm
这是自己写的最长的一次blog,算是比较完整,一直感觉自己真的不是不会开发,只是缺少机会,但话说机会又怎么会来找你呢!!并且说实话因为我的无知确实也错过了一些机会。但是成熟的人不会沉溺于过去,懂得释怀是一种成熟的表现,自己在未来的路上一定要走的坚定一些,可以不舒服,也可以忍受暂时的痛苦,但绝不能懦弱。
写于凌晨2016.6.23 3:17
致自己
南无大慈大悲观世音菩萨