jersey示例

Posted castielangel

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jersey示例相关的知识,希望对你有一定的参考价值。

package com.test;

import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.HttpUrlConnectorProvider;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import entity.ErrorInfo;

public class JerseyClient 
	
	private static final Logger log = LoggerFactory.getLogger(ResourceUtil.EMS);
			
	/**
	 * 
	 * @return HttpAuthenticationFeature
	 * */
	protected static HttpAuthenticationFeature getNetconfAuth() 
		return HttpAuthenticationFeature.basicBuilder().credentials(
				PropertiesUtil.getString("netconf_username"),
				PropertiesUtil.getString("netconf_password")).build();
	

	public static ErrorInfo post(String url, String input, boolean auth) 
		log.info("Post Method: url=, input=", url, input);
		Client client = ClientBuilder.newClient();
		WebTarget webTar = client.target(url);
		if (auth) 
			HttpAuthenticationFeature authen = getNetconfAuth();
			webTar = webTar.register(authen);
		
		Invocation.Builder invBuilder = webTar.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN);
		Response res = invBuilder.post(Entity.entity(input, MediaType.APPLICATION_JSON));
		
		ErrorInfo errorInfo = new ErrorInfo();
		errorInfo.setErrorCode(res.getStatus());
		String json = res.readEntity(String.class);
		errorInfo.setErrorDesc(json);
		String str = errorInfo.toString();
		if (log.isDebugEnabled()) 
			log.debug(str);
		 else 
			log.info(str.length()>512 ? str.substring(0, 511) : str);
		
		return errorInfo;
	
	
	public static ErrorInfo get(String url, boolean auth) 
		log.info("Get Method: url=", url);
		Client client = ClientBuilder.newClient();
		WebTarget webTar = client.target(url);
		if (auth) 
			HttpAuthenticationFeature authen = getNetconfAuth();
			webTar = webTar.register(authen);
		
		Invocation.Builder invBuilder = webTar.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN);
		Response res = invBuilder.get();
		
		ErrorInfo errorInfo = new ErrorInfo();
		errorInfo.setErrorCode(res.getStatus());
		String json = res.readEntity(String.class);
		errorInfo.setErrorDesc(json);
		String str = errorInfo.toString();
		if (log.isDebugEnabled()) 
			log.debug(str);
		 else 
			log.info(str.length()>512 ? str.substring(0, 511) : str);
		
		return errorInfo;
	
	
	public static String getWithJson(String url, boolean auth) 
		log.info("Get Method: url=", url);
		Client client = ClientBuilder.newClient();
		WebTarget webTar = client.target(url);
		if (auth) 
			HttpAuthenticationFeature authen = getNetconfAuth();
			webTar = webTar.register(authen);
		
		Invocation.Builder invBuilder = webTar.request(MediaType.APPLICATION_JSON,MediaType.TEXT_PLAIN);
		Response res = invBuilder.get();
		String json = res.readEntity(String.class);
		log.info("Get Method, body=", json);
		return json;
	
	
	public static ErrorInfo delete(String url, boolean auth) 
		log.info("Delete Method: url=", url);
		Client client = ClientBuilder.newClient();
		WebTarget webTar = client.target(url);
		if (auth) 
			HttpAuthenticationFeature authen = getNetconfAuth();
			webTar = webTar.register(authen);
		
		Invocation.Builder invBuilder = webTar.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN);
		Response res = invBuilder.delete();
		
		ErrorInfo errorInfo = new ErrorInfo();
		errorInfo.setErrorCode(res.getStatus());
		String json = res.readEntity(String.class);
		errorInfo.setErrorDesc(json);
		String str = errorInfo.toString();
		if (log.isDebugEnabled()) 
			log.debug(str);
		 else 
			log.info(str.length()>512 ? str.substring(0, 511) : str);
		
		return errorInfo;
	
	
	public static ErrorInfo patch(String url, String input, boolean auth) 
		log.info("Patch Method: url=, input=", url, input);
		Client client = ClientBuilder.newClient();
		WebTarget webTar = client.target(url);
		if (auth) 
			HttpAuthenticationFeature authen = getNetconfAuth();
			webTar = webTar.register(authen);
		
		Invocation.Builder invBuilder = webTar.request();
		Entity<String> entity = Entity.entity(input, "application/yang.patch+json");
		Response res = invBuilder.build("PATCH", entity)
					.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true).invoke();

		ErrorInfo errorInfo = new ErrorInfo();
		/* patch方法的返回结果在状态码中无法获取,必须解析返回的应答内容 */
		String json = res.readEntity(String.class);
		if (json.contains(ResourceUtil.ERRORS)) 
			errorInfo.setErrorCode(400);
		 else 
			errorInfo.setErrorCode(200);
		
		errorInfo.setErrorDesc(json);
		String str = errorInfo.toString();
		if (log.isDebugEnabled()) 
			log.debug(str);
		 else 
			log.info(str.length()>512 ? str.substring(0, 511) : str);
		
		return errorInfo;
	

	public static ErrorInfo put(String url, String input, boolean auth) 
		log.info("Put Method: url=, input=", url, input);
		Client client = ClientBuilder.newClient();
		WebTarget webTar = client.target(url);
		if (auth) 
			HttpAuthenticationFeature authen = getNetconfAuth();
			webTar = webTar.register(authen);
		
		Invocation.Builder invBuilder = webTar.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN);
		Response res = invBuilder.put(Entity.entity(input, MediaType.APPLICATION_JSON));
		
		ErrorInfo errorInfo = new ErrorInfo();
		errorInfo.setErrorCode(res.getStatus());
		String json = res.readEntity(String.class);
		errorInfo.setErrorDesc(json);
		String str = errorInfo.toString();
		if (log.isDebugEnabled()) 
			log.debug(str);
		 else 
			log.info(str.length()>512 ? str.substring(0, 511) : str);
		
		return errorInfo;
	
	
	public static String rpcPost(String url, String input, boolean auth) 
		log.info("rpcPost Method: url=, input=", url, input);
		Client client = ClientBuilder.newClient();
		WebTarget webTar = client.target(url);
		if (auth) 
			HttpAuthenticationFeature authen = getNetconfAuth();
			webTar = webTar.register(authen);
		
		Invocation.Builder invBuilder = webTar.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN);
		Response res = invBuilder.post(Entity.entity(input, MediaType.APPLICATION_JSON));
		
		ErrorInfo errorInfo = new ErrorInfo();
		errorInfo.setErrorCode(res.getStatus());
		errorInfo.setErrorDesc(res.getStatusInfo().getReasonPhrase());
		JSONObject returnJson = (JSONObject) JSONObject.toJSON(errorInfo);
//		returnJson.put("returnBody", res.readEntity(JSONObject.class));
		JSONObject resJson = null;
		try 
			resJson = res.readEntity(JSONObject.class);
		 catch (ProcessingException e) 
			log.error("rpcPost Exception:", e.getMessage(), e);
		 catch (IllegalStateException e) 
			log.error("rpcPost Exception:", e.getMessage(), e);
		
		returnJson.put("returnBody", resJson);
		log.info("rpcPost Method return:", returnJson.toJSONString());
		return returnJson.toJSONString();
	


	public static String devrRpcPost(String url, String input, boolean auth) 
		log.info("rpcPost Method: url=, input=", url, input);
		Client client = ClientBuilder.newClient();
		WebTarget webTar = client.target(url);
		if (auth) 
			HttpAuthenticationFeature authen = getNetconfAuth();
			webTar = webTar.register(authen);
		
		Invocation.Builder invBuilder = webTar.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN);
		Response res = invBuilder.post(Entity.entity(input, MediaType.APPLICATION_JSON));

		ErrorInfo errorInfo = new ErrorInfo();
		errorInfo.setErrorCode(res.getStatus());
		errorInfo.setErrorDesc(res.getStatusInfo().getReasonPhrase());
//		JSONObject returnJson = (JSONObject) JSONObject.toJSON(errorInfo);
//		returnJson.put("returnBody", res.readEntity(JSONObject.class));
		JSONObject jsonObject = new JSONObject();
		jsonObject.put(Constant.ERRORCODE,res.getStatus());
        JSONObject returnJson = (JSONObject) JSONObject.toJSON(errorInfo);
		log.info("rpcPost Method return:", returnJson.toJSONString());
		return jsonObject.toJSONString();
	

  

以上是关于jersey示例的主要内容,如果未能解决你的问题,请参考以下文章

Java Restful框架:Jersey入门示例(官方例子)

jersey示例

Jersey JAX-RS + Spring 安全应用示例

如何在 tomcat 7 中的 Jersey 2 中使用异步回调

笔记:Jersey REST 传输格式

如何使用 Jersey 获取完整的 REST 请求正文?