本文目錄一覽:
- 1、如何在Java中 提供 RESTful Web 服務
- 2、java 調用 rest 接口 怎麼寫請求行的信息
- 3、ResteasyClient 怎麼設置請求的編碼格式
- 4、如何在java中發起http和https請求
如何在Java中 提供 RESTful Web 服務
通過REST風格體系架構,請求和響應都是基於資源表示的傳輸來構建的。資源是通過全局ID來標識的,這些ID一般使用的是一個統一資源標識符(URI)。客戶端應用使用HTTP方法(如,GET、POST、PUT或DELETE)來操作一個或多個資源。通常,GET是用於獲取或列出一個或多個資源,POST用於創建,PUT用於更新或替換,而DELETE則用於刪除資源。
例如,GET http //host/context/employees/12345將獲取ID為12345的員工的表示。這個響應表示可以是包含詳細的員工信息的XML或ATOM,或者是具有更好UI的JSP/HTML頁面。您看到哪種表示方式取決於服務器端實現和您的客戶端請求的MIME類型。
RESTful Web Service是一個使用HTTP和REST原理實現的Web Service。通常,一個RESTful Web Service將定義基本資源URI、它所支持的表示/響應MIME,以及它所支持的操作。
本文將介紹如何使用Spring創建Java實現的服務器端RESTful Web Services。這個例子將使用瀏覽器、curl和Firefox插件RESTClient作為發出請求的客戶端。
本文假定您是熟悉REST基本知識的。
Spring 3的REST支持
在Spring框架支持REST之前,人們會使用其他幾種實現技術來創建Java RESTful Web Services,如Restlet、RestEasy和Jersey。Jersey是其中最值得注意的,它是JAX-RS(JSR 311)的參考實現。
Spring是一個得到廣泛應用的Java EE框架,它在版本3以後就增加了RESTful Web Services開發的支持。雖然,對REST的支持並不是JAX-RS的一種實現,但是它具有比標準定義更多的特性。REST支持被無縫整合到Spring的MVC層,它可以很容易應用到使用Spring構建的應用中。
Spring REST支持的主要特性包括:
注釋,如@RequestMapping 和 @PathVariable,支持資源標識和URL映射
ContentNegotiatingViewResolver支持為不同的MIME/內容類型使用不同的表示方式
使用相似的編程模型無縫地整合到原始的 MVC 層
創建一個示例RESTful Web Service
本節中的例子將演示Spring 3環境的創建過程,並創建一個可以部署到Tomcat中的“Hello World”應用。然後我們再完成一個更複雜的應用來了解Spring 3 REST支持的重要概念,如多種MIME類型表示支持和JAXB支持。另外,本文還使用一些代碼片斷來幫助理解這些概念。
Hello World:使用Spring 3 REST支持
要創建這個例子所使用的開發環境,您需要:
IDE:Eclipse IDE for JEE (v3.4+)
Java SE5 以上
Web 容器:Apache Tomcat 6.0(Jetty或其他容器也可)
Spring 3框架(v3.0.3是本文編寫時的最新版本)
其他程序庫:JAXB 2、JSTL、commons-logging
在 Eclipse 中創建一個Web應用,然後設置Tomcat 6作為它的運行環境。然後,您需要設置web.xml文件來激活Spring
WebApplicationContext。這個例子將Spring bean配置分成兩個文件:rest-servlet.xml 包含與MVC/REST有關的配置,rest-context.xml包含服務級別的配置(如數據源 beans)。清單 1 顯示了web.xml中的Spring配置的部分。
清單 1. 在web.xml中激活Spring WebApplicationContext
以下是引用片段:
contextConfigLocation
/WEB-INF/rest-context.xml
!– This listener will load other application context file in addition to
rest-servlet.xml —
org.springframework.web.context.ContextLoaderListener
rest
org.springframework.web.servlet.DispatcherServlet
1
rest
/service/*
在rest-servlet.xml文件中創建Spring MVC的相關配置(Controller、View、View Resolver)。清單 2 顯示了其中最重要的部分。
清單 2. 在rest-servlet.xml文件中創建Spring MVC配置
以下是引用片段:
bean class=”org.springframework.web.servlet.mvc.annotation
.DefaultAnnotationHandlerMapping” /
bean class=”org.springframework.web.servlet.mvc.annotation
.AnnotationMethodHandlerAdapter” /
bean id=”jaxbMarshaller”
class=”org.springframework.oxm.jaxb.Jaxb2Marshaller”
dw.spring3.rest.bean.Employee
dw.spring3.rest.bean.EmployeeList
bean id=”employees” class=
“org.springframework.web.servlet.view.xml.MarshallingView”
bean id=”viewResolver” class=
“org.springframework.web.servlet.view.BeanNameViewResolver” /
上面的代碼中:
Component-scan啟用對帶有Spring注釋的類進行自動掃描,在實踐中,它將檢查控制器類中所定義的@Controller注釋。
DefaultAnnotationHanlderMappings和AnnotationMethodHandlerAdapter使用@ReqeustMapping注釋的類或函數的beans由Spring處理這個注釋將在下一節進行詳細介紹。
Jaxb2Mashaller定義使用JAXB 2進行對象XML映射(OXM)的編組器(marshaller)和解組器(unmarshaller )
MashallingView定義一個使用Jaxb2Mashaller的XML表示view
BeanNameViewResolver使用用戶指定的bean名稱定義一個視圖解析器
本例將使用名為“employees”的MarshallingView。
這樣就完成了Spring的相關配置。下一步是編寫一個控制器來處理用戶請求。清單3顯示的是控制器類。
java 調用 rest 接口 怎麼寫請求行的信息
package com.demo;
import jaimg id=”selectsearch-icon” src=”” alt=”搜索”va.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.xml.bind.DatatypeConverter;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class restTest {
public static voidmain(String[] args) {
try {
DefaultHttpClient Client = newDefaultHttpClient();
HttpGet httpGet = newHttpGet(“你的地址”);
String encoding =DatatypeConverter.printBase64Binary(“admin:admin”.getBytes(“UTF-8”));
httpGet.setHeader(“Authorization”, “Basic ” +encoding);
HttpResponse response = Client.execute(httpGet);
System.out.println(“response =” + response);
BufferedReader breader = newBufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder responseString = newStringBuilder();
String line = “”;
while ((line = breader.readLine()) !=null) {
responseString.append(line);
}
breader.close();
String repsonseStr =responseString.toString();
System.out.println(“repsonseStr =” + repsonseStr);
} catch (IOException e) {
e.printStackTrace();
}
}
}
ResteasyClient 怎麼設置請求的編碼格式
package com.supermap.earth.rims.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
public class CustomerHttpClient {
private static final String TAG = “CustomerHttpClient”;
private static final String ENCODING = HTTP.UTF_8;
private static HttpClient customerHttpClient;
private CustomerHttpClient() {
}
public static synchronized HttpClient getHttpClient() {
if (null == customerHttpClient) {
HttpParams params = new BasicHttpParams();
// 設置一些基本參數
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, ENCODING);
HttpProtocolParams.setUseExpectContinue(params, true);
HttpProtocolParams
.setUserAgent(
params,
“Mozilla/5.0(Linux;U;Android 2.2.1;en-us;Nexus One Build.FRG83) “
+ “AppleWebKit/553.1(KHTML,like Gecko) Version/4.0 Mobile Safari/533.1”);
ConnManagerParams.setTimeout(params, 5000);
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 4000);
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme(“http”, PlainSocketFactory
.getSocketFactory(), 80));
schReg.register(new Scheme(“https”, SSLSocketFactory
.getSocketFactory(), 443));
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
params, schReg);
customerHttpClient = new DefaultHttpClient(conMgr, params);
}
return customerHttpClient;
}
public static String post(String url, NameValuePair… params) {
try {
// 編碼參數
ListNameValuePair formparams = new ArrayListNameValuePair(); // 請求參數
for (NameValuePair p : params) {
formparams.add(p);
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams,
ENCODING);
// 創建POST請求
HttpPost request = new HttpPost(url);
request.setEntity(entity);
// 發送請求
HttpClient client = getHttpClient();
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
throw new RuntimeException(“請求失敗”);
}
HttpEntity resEntity = response.getEntity();
return (resEntity == null) ? null : EntityUtils.toString(resEntity,
ENCODING);
} catch (UnsupportedEncodingException e) {
return null;
} catch (ClientProtocolException e) {
return null;
} catch (IOException e) {
throw new RuntimeException(“連接失敗”, e);
}
}
/**
* httpclient不直接支持及mime multipart方式上傳附件,需要引入第三方類庫
* D:\Downloads\android\apache mime\apache-mime4j-0.6.jar
* D:\Downloads\android\apache mime\commons-io-2.1.jar
* D:\Downloads\android\apache mime\httpmime-4.0.jar
*/
public static boolean httpPostUpload(String serverUrl, String headContent,
String fileName) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(serverUrl);
// multipart實體
MultipartEntity entity = new MultipartEntity();
entity.addPart(“picMsg”, new StringBody(headContent));
entity.addPart(“pic”, new FileBody(new File(fileName)));
post.setEntity(entity);
HttpResponse resp = client.execute(post);
if (resp.getStatusLine().getStatusCode() == 200) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
// ////////////////////////////////////////////////////////////////
/**
* 獲取圖片流
*
* @param uri
* 圖片地址
*
* @return
* @throws MalformedURLException
*/
public static InputStream GetImageByUrl(String uri)
throws MalformedURLException {
URL url = new URL(uri);
URLConnection conn;
InputStream is;
try {
conn = url.openConnection();
conn.connect();
is = conn.getInputStream();
return is;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void saveFile(InputStream fis, String saveDir)
throws Exception {
FileOutputStream fos = new FileOutputStream(new File(saveDir));
byte[] b = new byte[1];
while (fis.read(b) != -1) {
fos.write(b);
fos.flush();
}
fis.close();
fos.close();
}
}
如何在java中發起http和https請求
1.寫http請求方法
[java] view plain copy
//處理http請求 requestUrl為請求地址 requestMethod請求方式,值為”GET”或”POST”
public static String httpRequest(String requestUrl,String requestMethod,String outputStr){
StringBuffer buffer=null;
try{
URL url=new URL(requestUrl);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod(requestMethod);
conn.connect();
//往服務器端寫內容 也就是發起http請求需要帶的參數
if(null!=outputStr){
OutputStream os=conn.getOutputStream();
os.write(outputStr.getBytes(“utf-8”));
os.close();
}
//讀取服務器端返回的內容
InputStream is=conn.getInputStream();
InputStreamReader isr=new InputStreamReader(is,”utf-8″);
BufferedReader br=new BufferedReader(isr);
buffer=new StringBuffer();
String line=null;
while((line=br.readLine())!=null){
buffer.append(line);
}
}catch(Exception e){
e.printStackTrace();
}
return buffer.toString();
}
2.測試。
[java] view plain copy
public static void main(String[] args){
String s=httpRequest(“”,”GET”,null);
System.out.println(s);
}
輸出結果為的源代碼,說明請求成功。
註:1).第一個參數url需要寫全地址,即前邊的http必須寫上,不能只寫這樣的。
2).第二個參數是請求方式,一般接口調用會給出URL和請求方式說明。
3).第三個參數是我們在發起請求的時候傳遞參數到所要請求的服務器,要傳遞的參數也要看接口文檔確定格式,一般是封裝成json或xml.
4).返回內容是String類,但是一般是有格式的json或者xml。
二:發起https請求。
1.https是對鏈接加了安全證書SSL的,如果服務器中沒有相關鏈接的SSL證書,它就不能夠信任那個鏈接,也就不會訪問到了。所以我們第一步是自定義一個信任管理器。自要實現自帶的X509TrustManager接口就可以了。
[java] view plain copy
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;
public class MyX509TrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public X509Certificate[] getAcceptedIssuers() {
// TODO Auto-generated method stub
return null;
}
}
註:1)需要的包都是java自帶的,所以不用引入額外的包。
2.)可以看到裡面的方法都是空的,當方法為空是默認為所有的鏈接都為安全,也就是所有的鏈接都能夠訪問到。當然這樣有一定的安全風險,可以根據實際需要寫入內容。
2.編寫https請求方法。
[java] view plain copy
/*
* 處理https GET/POST請求
* 請求地址、請求方法、參數
* */
public static String httpsRequest(String requestUrl,String requestMethod,String outputStr){
StringBuffer buffer=null;
try{
//創建SSLContext
SSLContext sslContext=SSLContext.getInstance(“SSL”);
TrustManager[] tm={new MyX509TrustManager()};
//初始化
sslContext.init(null, tm, new java.security.SecureRandom());;
//獲取SSLSocketFactory對象
SSLSocketFactory ssf=sslContext.getSocketFactory();
URL url=new URL(requestUrl);
HttpsURLConnection conn=(HttpsURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod(requestMethod);
//設置當前實例使用的SSLSoctetFactory
conn.setSSLSocketFactory(ssf);
conn.connect();
//往服務器端寫內容
if(null!=outputStr){
OutputStream os=conn.getOutputStream();
os.write(outputStr.getBytes(“utf-8”));
os.close();
}
//讀取服務器端返回的內容
InputStream is=conn.getInputStream();
InputStreamReader isr=new InputStreamReader(is,”utf-8″);
BufferedReader br=new BufferedReader(isr);
buffer=new StringBuffer();
String line=null;
while((line=br.readLine())!=null){
buffer.append(line);
}
}catch(Exception e){
e.printStackTrace();
}
return buffer.toString();
}
可見和http訪問的方法類似,只是多了SSL的相關處理。
3.測試。先用http請求的方法訪問,再用https的請求方法訪問,進行對比。
http訪問:
[java] view plain copy
public static void main(String[] args){
String s=httpRequest(“”,”GET”,null);
System.out.println(s);
}
結果為:
https訪問:
[java] view plain copy
public static void main(String[] args){
String s=httpsRequest(“”,”GET”,null);
System.out.println(s);
}
結果為:
可見https的鏈接一定要進行SSL的驗證或者過濾之後才能夠訪問。
三:https的另一種訪問方式——導入服務端的安全證書。
1.下載需要訪問的鏈接所需要的安全證書。 以這個網址為例。
1)在瀏覽器上訪問。
2)點擊上圖的那個打了×的鎖查看證書。
3)選擇複製到文件進行導出,我們把它導入到java項目所使用的jre的lib文件下的security文件夾中去,我的是這個路徑。D:\Program Files (x86)\Java\jre8\lib\security
註:中間需要選導出格式,就選默認的就行,還需要命名,我命名的是12306.
2.打開cmd,進入到java項目所使用的jre的lib文件下的security目錄。
3.在命令行輸入 Keytool -import -alias 12306 -file 12306.cer -keystore cacerts
4.回車後會讓輸入口令,一般默認是changeit,輸入時不顯示,輸入完直接按回車,會讓確認是否信任該證書,輸入y,就會提示導入成功。
5.導入成功後就能像請求http一樣請求https了。
測試:
[java] view plain copy
public static void main(String[] args){
String s=httpRequest(“”,”GET”,null);
System.out.println(s);
}
結果:
現在就可以用http的方法請求https了。
註:有時候這一步還是會出錯,那可能是jre的版本不對,我們右鍵run as——run configurations,選擇證書所在的jre之後再運行。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/278380.html