去實習公司,組長讓搭個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
致自己
南無大慈大悲觀世音菩薩