Dubbo接口RestFul接口

Posted java游泳圈

tags:

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

如果你不做出改变,那什么都不会发生改变


记录工作中遇到的问题和解决方法,与君共勉


1.使用dubbo或者普通类调用rest接口

package com.itcast.controller;
import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.itcast.domain.Account;import org.springframework.core.ParameterizedTypeReference;import org.springframework.http.*;import org.springframework.http.client.SimpleClientHttpRequestFactory;import org.springframework.http.converter.HttpMessageConverter;import org.springframework.web.client.RestClientException;import org.springframework.web.client.RestTemplate;
import java.util.List;


public class DubboToRestFul {
public static void main(String[] args) { TestGet(); TestPost(); }
private static Account TestPost() { RestTemplate restTemplate = new RestTemplate(setRestTimeOut());
ParameterizedTypeReference<String> typeReference = new ParameterizedTypeReference<String>(){};
String url ="http://127.0.0.1:8080/test/dubbo/findAll";
HttpHeaders headers = new HttpHeaders(); headers.add("Accept","application/json;charset=UTF-8");
Account account = new Account(); account.setAid(1212); account.setNumber("afadfa");
HttpEntity<Object> httpEntity = new HttpEntity<>(account,headers); try { ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, httpEntity, String.class); Account account1 = JSON.parseObject(stringResponseEntity.getBody(), Account.class); return account1; } catch (RestClientException e) { e.printStackTrace(); } return null;
}
private static Account TestGet() { RestTemplate restTemplate = new RestTemplate(setRestTimeOut());
ParameterizedTypeReference<String> typeReference = new ParameterizedTypeReference<String>(){};
String url ="http://127.0.0.1:8080/test/dubbo/findAll";
HttpHeaders headers = new HttpHeaders(); headers.add("Accept","application/json;charset=UTF-8");
HttpEntity<Object> httpEntity = new HttpEntity<>(headers); ResponseEntity<String> exchange = null; try { exchange = restTemplate.exchange(url, HttpMethod.GET, httpEntity, typeReference);
Account account = JSON.parseObject(exchange.getBody(), Account.class); return account; } catch (RestClientException e) { e.printStackTrace(); } return null;
}
/** * restFul接口访问超时设置 * @return */ private static SimpleClientHttpRequestFactory setRestTimeOut() { int resttimeout = 5; String rest_time="5"; try { resttimeout = Integer.parseInt(rest_time); } catch (NumberFormatException e) { } SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout(resttimeout * 1000);
return requestFactory; }}


2.只调用了get方法,是另一中方法,说是方法也有点过了,就是调用不同的API而已

package com.itcast.controller;
import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import org.springframework.http.HttpHeaders;import org.springframework.http.ResponseEntity;import org.springframework.web.client.RestTemplate;
public class DubboToRestFul2 { public static void main(String[] args) { TestGet(); }
private static void TestGet() { RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Accept", "application/json;charset=UTF-8"); String url = "http://127.0.0.1:8080/uuu/tt"; ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class); String body = forEntity.getBody(); JSONObject jsonObject = JSON.parseObject(body); String string = jsonObject.getString(""); System.out.println(string);

}}


以上是关于Dubbo接口RestFul接口的主要内容,如果未能解决你的问题,请参考以下文章

Dubbo 使用rest协议发布http服务

Rest在Dubbo中开发REST风格的远程调用(RESTful Remoting)

Springboot security oauth2 jwt实现权限控制,实现微服务获取当前用户信息

现在有一个SSO的Restful 的接口,用java代码具体怎么调用这个接口啊,需要传参啊?

jmeter测试dubbo接口

java后台接口怎么方便返回restful数据