stringutils 用哪个包 apache spring_spring整合mq、jsonp跨越、httpclient工具的使用
訓(xùn)練大綱(第087天)
大家如果想快速有效的學(xué)習(xí),思想核心是“以建立知識(shí)體系為核心”,具體方法是“守破離”。確保老師課堂上做的操作,反復(fù)練習(xí)直到熟練。
第173次(ActiveMQ)
學(xué)習(xí)主題:ActiveMQ
學(xué)習(xí)目標(biāo):
1 掌握什么是spring整合mq
2 掌握jsonp
對(duì)應(yīng)視頻:
http://www.itbaizhan.cn/course/id/85.html
對(duì)應(yīng)文檔:
無
對(duì)應(yīng)作業(yè)
Spring整合ActiveMQ
需求:
1)在 producer 中創(chuàng)建 Users 對(duì)象
2)將 Users 對(duì)象傳遞到 ActiveMQ 中
3)在 Consumer 中獲取 Users 對(duì)象并在控制臺(tái)打印
<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>
<parent>
<groupId>com.bjsxt</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.bjsxt</groupId>
<artifactId>spring-activemq-producer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- ActiveMQ客戶端完整jar包依賴 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
</dependency>
<!-- ActiveMQ和Spring整合配置文件標(biāo)簽處理jar包依賴 -->
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
</dependency>
<!-- Spring-JMS插件相關(guān)jar包依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-jms-pool</artifactId>
</dependency>
<!-- 單元測(cè)試 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- 日志處理 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!-- JSP相關(guān) -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<path>/</path>
<port>8080</port>
</configuration>
</plugin>
</plugins>
</build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 需要?jiǎng)?chuàng)建一個(gè)連接工廠,連接ActiveMQ. ActiveMQConnectionFactory. 需要依賴ActiveMQ提供的amq標(biāo)簽 -->
<!-- amq:connectionFactory 是bean標(biāo)簽的子標(biāo)簽, 會(huì)在spring容器中創(chuàng)建一個(gè)bean對(duì)象. 可以為對(duì)象命名.
類似: <bean id="" class="ActiveMQConnectionFactory"></bean> -->
<amq:connectionFactory brokerURL="tcp://192.168.240.134:61616"
userName="admin" password="admin" id="amqConnectionFactory" />
<!-- 配置池化的ConnectionFactory。 為連接ActiveMQ的connectionFactory提供連接池 -->
<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactoryBean">
<property name="connectionFactory" ref="amqConnectionFactory"></property>
<property name="maxConnections" value="10"></property>
</bean>
<!-- spring管理JMS相關(guān)代碼的時(shí)候,必須依賴jms標(biāo)簽庫(kù). spring-jms提供的標(biāo)簽庫(kù). -->
<!-- 定義Spring-JMS中的連接工廠對(duì)象 CachingConnectionFactory - spring框架提供的連接工廠對(duì)象.
不能真正的訪問MOM容器. 類似一個(gè)工廠的代理對(duì)象. 需要提供一個(gè)真實(shí)工廠,實(shí)現(xiàn)MOM容器的連接訪問. -->
<!-- 配置有緩存的ConnectionFactory,session的緩存大小可定制。 -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
<property name="sessionCacheSize" value="3"></property>
</bean>
<!-- JmsTemplate配置 -->
<bean id="template" class="org.springframework.jms.core.JmsTemplate">
<!-- 給定連接工廠, 必須是spring創(chuàng)建的連接工廠. -->
<property name="connectionFactory" ref="connectionFactory"></property>
<!-- 可選 - 默認(rèn)目的地命名 -->
<property name="defaultDestinationName" value="test-spring"></property>
</bean>
</beans>
<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>
<parent>
<groupId>com.bjsxt</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.bjsxt</groupId>
<artifactId>spring-activemq-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- activemq客戶端 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
</dependency>
<!-- spring框架對(duì)JMS標(biāo)準(zhǔn)的支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<!-- ActiveMQ和spring整合的插件 -->
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
</dependencies>
</project>
1.創(chuàng)建 spring-activemq-consumer
是一個(gè) jar 工程
2. 修改 POM 文件
3. 整合 ActiveMQ
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd">
<!-- 需要?jiǎng)?chuàng)建一個(gè)連接工廠,連接ActiveMQ. ActiveMQConnectionFactory. 需要依賴ActiveMQ提供的amq標(biāo)簽 -->
<!-- amq:connectionFactory 是bean標(biāo)簽的子標(biāo)簽, 會(huì)在spring容器中創(chuàng)建一個(gè)bean對(duì)象. 可以為對(duì)象命名.
類似: <bean id="" class="ActiveMQConnectionFactory"></bean> -->
<amq:connectionFactory brokerURL="tcp://192.168.70.151:61616"
userName="admin" password="admin" id="amqConnectionFactory" />
<!-- spring管理JMS相關(guān)代碼的時(shí)候,必須依賴jms標(biāo)簽庫(kù). spring-jms提供的標(biāo)簽庫(kù). -->
<!-- 定義Spring-JMS中的連接工廠對(duì)象 CachingConnectionFactory - spring框架提供的連接工廠對(duì)象.
不能真正的訪問MOM容器. 類似一個(gè)工廠的代理對(duì)象. 需要提供一個(gè)真實(shí)工廠,實(shí)現(xiàn)MOM容器的連接訪問. -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
<property name="sessionCacheSize" value="3"></property>
</bean>
<!-- 注冊(cè)監(jiān)聽器 -->
<!-- 開始注冊(cè)監(jiān)聽. 需要的參數(shù)有: acknowledge - 消息確認(rèn)機(jī)制 container-type - 容器類型 default|simple
simple:SimpleMessageListenerContainer最簡(jiǎn)單的消息監(jiān)聽器容器,只能處理固定數(shù)量的JMS會(huì)話,且不支持事務(wù)。 default:DefaultMessageListenerContainer是一個(gè)用于異步消息監(jiān)聽器容器
,且支持事務(wù) destination-type - 目的地類型. 使用隊(duì)列作為目的地. connection-factory - 連接工廠, spring-jms使用的連接工廠,必須是spring自主創(chuàng)建的
不能使用三方工具創(chuàng)建的工程. 如: ActiveMQConnectionFactory. -->
<jms:listener-container acknowledge="auto"
container-type="default" destination-type="queue" connection-factory="connectionFactory">
<!-- 在監(jiān)聽器容器中注冊(cè)某監(jiān)聽器對(duì)象. destination - 設(shè)置目的地命名 ref - 指定監(jiān)聽器對(duì)象 -->
<jms:listener destination="test-spring" ref="myListener" />
</jms:listener-container>
</beans>
Jsonp(JSON with Padding) 是 json 的一種"使用模式",可以讓網(wǎng)頁(yè)從別的域名(網(wǎng)站) 那獲取資料,即跨域讀取數(shù)據(jù)。
為什么我們從不同的域(網(wǎng)站)訪問數(shù)據(jù)需要一個(gè)特殊的技術(shù)(JSONP )呢?
這是因?yàn)橥床呗浴?/p>
JSONP 的優(yōu)點(diǎn)是:它不像 XMLHttpRequest 對(duì)象實(shí)現(xiàn)的 Ajax 請(qǐng)求那樣受到同源策略的 限制;它的兼容性更好,在更加古老的瀏覽器中都 可以運(yùn)行,不需要 XMLHttpRequest 或 ActiveX 的支持;并且在請(qǐng)求完畢后可以通過調(diào)用 callback 的方式回傳結(jié)果。 JSONP 的缺點(diǎn)則是:它只支持 GET 請(qǐng)求而不支持 POST 等其它類型的 HTTP 請(qǐng)求;它只支持跨域 HTTP 請(qǐng)求這種情況,不能解決不同域的兩個(gè)頁(yè)面之間如何進(jìn)行 JavaScript 調(diào)用 的問題。
跨域是指一個(gè)域(網(wǎng)站)下的文檔或腳本試圖去請(qǐng)求另一個(gè)域(網(wǎng)站)下的資源。
同源策略/SOP(Same origin policy)是一種約定,由 Netscape 公司 1995 年引入瀏覽器, 它是瀏覽器最核心也最基本的安全功能,現(xiàn)在所有支持 JavaScript 的瀏覽器都會(huì)使用這個(gè)策 略。如果缺少了同源策略,瀏覽器很容易受到 XSS、CSFR 等攻擊。所謂同源是指"協(xié)議+域名+端口"三者相同,即便兩個(gè)不同的域名指向同一個(gè) ip 地址,也非同源。
分享/講解/擴(kuò)展思考
點(diǎn)名提問從第一節(jié)課到最后一節(jié)課分別學(xué)到了什么,直到同學(xué)們把所有的知識(shí)點(diǎn)都說出來并且保證無誤。
第174次(jsonp和httpclient)
學(xué)習(xí)主題:ActiveMQ
學(xué)習(xí)目標(biāo):
1 掌握jsonp跨域
2 掌握httpclient工具的使用
對(duì)應(yīng)視頻:
http://www.itbaizhan.cn/course/id/85.html
對(duì)應(yīng)文檔:
無
對(duì)應(yīng)作業(yè)
在 ajax 中請(qǐng)求方式有所改變
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="/js/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function() {
$("#but").click(
function() {
$.ajax({
type : "get",
url : "http://localhost:9090/user/findUser",
dataType : "jsonp",
jsonp : "callback",
success : function(data) {
alert(data);
var str = "";
for (i = 0; i < data.length; i++) {
str += data[i].userid + " " + data[i].username
+ " " + data[i].userage + " ";
}
$("#show").html(str);
}
});
});
});
</script>
</head>
<body>
<span id="show"></span>
<input type="button" value="ok" id="but" />
</body>
</html>
package com.bjsxt.web.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bjsxt.commons.JsonUtils;
import com.bjsxt.pojo.Users;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/findUser")
@ResponseBody
public Object findUser(String callback){
Users user = new Users(1, "admin", 20);
Users user1 = new Users(2, "zhangsan", 22);
Users user2 = new Users(3, "lisi", 24);
List<Users> list = new ArrayList<>();
list.add(user);
list.add(user1);
list.add(user2);
//json轉(zhuǎn)換
/*String json = JsonUtils.objectToJson(list);*/
MappingJacksonValue mv = new MappingJacksonValue(list);//json轉(zhuǎn)換
mv.setJsonpFunction(callback);//字符串list和callback拼接
return mv;
}
}
HttpClient 是 Apache Jakarta Common 下的子項(xiàng)目,可以用來提供高效的、最新的、 功能豐富的支持 HTTP 協(xié)議的客戶端編程工具包,并且它支持 HTTP 協(xié)議最新的版本和 建議。
HTTP 協(xié)議可能是現(xiàn)在 Internet 上使用得最多、最重要的協(xié)議了,越來越多的 Java 應(yīng)用程序需要直接通過 HTTP 協(xié)議來訪問網(wǎng)絡(luò)資源。雖然在 JDK 的 java net 包中已經(jīng)提 供了訪問 HTTP 協(xié)議的基本功能,但是對(duì)于大部分應(yīng)用程序來說,JDK 庫(kù)本身提供的功能 還不夠豐富和靈活
package com.bjsxt.test;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpClientTest {
public static void main(String[] args) throws Exception {
HttpClientTest.doGet();
}
/**
* get請(qǐng)求不帶參數(shù)
*
* @throws Exception
* @throws ClientProtocolException
*/
public static void doGet() throws Exception {
// 創(chuàng)建HttpClient對(duì)象
CloseableHttpClient client = HttpClients.createDefault();
// 創(chuàng)建get請(qǐng)求對(duì)象。在請(qǐng)求中輸入url
HttpGet get = new HttpGet("http://www.baidu.com");
// 發(fā)送請(qǐng)求并返回響應(yīng)
CloseableHttpResponse res = client.execute(get);
// 處理響應(yīng)
// 獲取響應(yīng)的狀態(tài)碼
int code = res.getStatusLine().getStatusCode();
System.out.println(code);
// 獲取響應(yīng)的內(nèi)容
HttpEntity entity = res.getEntity();
String content = EntityUtils.toString(entity, "utf-8");
System.out.println(content);
// 關(guān)閉鏈接
client.close();
}
}
/**
* Get 請(qǐng)求帶參數(shù)
* @throws Exception
*/
public static void doGetParam() throws
Exception{
CloseableHttpClient client =
HttpClients.createDefault();
//創(chuàng)建一個(gè)封裝 URI 的對(duì)象。在該對(duì)象中可以給定請(qǐng)
求參數(shù)
URIBuilder bui = new
URIBuilder("https://www.sogou.com/web");
bui.addParameter("query", "西游記");
//創(chuàng)建一個(gè) Get 請(qǐng)求對(duì)象
HttpGet get = new HttpGet(bui.build());
//發(fā)送請(qǐng)求,并返回響應(yīng)
CloseableHttpResponse res =
client.execute(get);
//處理響應(yīng)
//獲取響應(yīng)的狀態(tài)碼
int code =
res.getStatusLine().getStatusCode();
System.out.println(code);
//獲取響應(yīng)的內(nèi)容
HttpEntity entity = res.getEntity();
String content =
EntityUtils.toString(entity,"utf-8");
System.out.println(content);
//關(guān)閉連接
client.close();
}
/**
* 發(fā)送 POST 請(qǐng)求不帶參數(shù)
*/
public static void doPostTest()throws
Exception{
CloseableHttpClient client =
HttpClients.createDefault();
HttpPost post = new
HttpPost("http://localhost:8080/test/post");
CloseableHttpResponse res =
client.execute(post);
//處理響應(yīng)
//獲取響應(yīng)的狀態(tài)碼
int code =
res.getStatusLine().getStatusCode();
System.out.println(code);
//獲取響應(yīng)的內(nèi)容
HttpEntity entity = res.getEntity();
String content =
EntityUtils.toString(entity,"utf-8");
System.out.println(content);
//關(guān)閉連接
client.close();
}
/**
* 發(fā)送 POST 請(qǐng)求帶參數(shù)
*/
public static void doPostParamTest()throws
Exception{
CloseableHttpClient client =
HttpClients.createDefault();
HttpPost post = new
HttpPost("http://localhost:8080/test/post/param");
//給定參數(shù)
List<BasicNameValuePair> list = new
ArrayList<>();
list.add(new BasicNameValuePair("name", "張
三豐"));
list.add(new BasicNameValuePair("pwd",
"zhangsanfeng"));
//將參數(shù)做字符串的轉(zhuǎn)換
StringEntity entity = new
UrlEncodedFormEntity(list,"utf-8");
//向請(qǐng)求中綁定參數(shù)
post.setEntity(entity);
//處理響應(yīng)
CloseableHttpResponse res =
client.execute(post);
//獲取響應(yīng)的狀態(tài)碼
int code =
res.getStatusLine().getStatusCode();
System.out.println(code);
//獲取響應(yīng)的內(nèi)容
HttpEntity en = res.getEntity();
String content =
EntityUtils.toString(en,"utf-8");
System.out.println(content);
//關(guān)閉連接
client.close();
}
/**
* 發(fā)送 POST 請(qǐng)求帶 JSON 格式參數(shù)
*/
public static void doPostParamJsonTest()throws
Exception{
CloseableHttpClient client =
HttpClients.createDefault();
HttpPost post = new
HttpPost("http://localhost:8080/test/post/param/js
on");
String json ="{"name":"張三豐
","pwd":"zhangsanfeng"}";
StringEntity entity = new StringEntity(json,
ContentType.APPLICATION_JSON);
//向請(qǐng)求中綁定參數(shù)
post.setEntity(entity);
//處理響應(yīng)
CloseableHttpResponse res =
client.execute(post);
//獲取響應(yīng)的狀態(tài)碼
int code =
res.getStatusLine().getStatusCode();
System.out.println(code);
//獲取響應(yīng)的內(nèi)容
HttpEntity en = res.getEntity();
String content =
EntityUtils.toString(en,"utf-8");
System.out.println(content);
//關(guān)閉連接
client.close();
}
分享/講解/擴(kuò)展思考
點(diǎn)名提問從第一節(jié)課到最后一節(jié)課分別學(xué)到了什么,直到同學(xué)們把所有的知識(shí)點(diǎn)都說出來并且保證無誤。
總結(jié)
以上是生活随笔為你收集整理的stringutils 用哪个包 apache spring_spring整合mq、jsonp跨越、httpclient工具的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全字库说文解字字体_【180期】可商用字
- 下一篇: 三菱m70刀杯上下m代码_加工中心常用G