restful接口定义_主板上的spi接口接什么

restful接口定义_主板上的spi接口接什么由于在实际项目中碰到的restful服务,参数都以json为准。这里我获取的接口和传入的参数都是json字符串类型。发布restful服务可参照文章http://www.cnblogs.com/jav

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

由于在实际项目中碰到的restful服务,参数都以json为准。这里我获取的接口和传入的参数都是json字符串类型。发布restful服务可参照文章http://www.cnblogs.com/jave1ove/p/7277861.html,以下接口调用基于此服务。

基于发布的Restful服务,下面总结几种常用的调用方法。

(1)Jersey API

package com.restful.client;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.restful.entity.PersonEntity;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

import javax.ws.rs.core.MediaType;

/**
 * Created by XuHui on 2017/8/7.
 */
public class JerseyClient {
    private static String REST_API = "http://localhost:8080/jerseyDemo/rest/JerseyService";
    public static void main(String[] args) throws Exception {
        getRandomResource();
        addResource();
        getAllResource();
    }

    public static void getRandomResource() {
        Client client = Client.create();
        WebResource webResource = client.resource(REST_API + "/getRandomResource");
        ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).accept("application/json").get(ClientResponse.class);
        String str = response.getEntity(String.class);
        System.out.print("getRandomResource result is : " + str + "\n");
    }

    public static void addResource() throws JsonProcessingException {
        Client client = Client.create();
        WebResource webResource = client.resource(REST_API + "/addResource/person");
        ObjectMapper mapper = new ObjectMapper();
        PersonEntity entity = new PersonEntity("NO2", "Joker", "http://");
        ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, mapper.writeValueAsString(entity));
        System.out.print("addResource result is : " + response.getEntity(String.class) + "\n");
    }

    public static void getAllResource() {
        Client client = Client.create();
        WebResource webResource = client.resource(REST_API + "/getAllResource");
        ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).accept("application/json").get(ClientResponse.class);
        String str = response.getEntity(String.class);
        System.out.print("getAllResource result is : " + str + "\n");
    }
}

结果:

getRandomResource result is : {“id”:”NO1″,”name”:”Joker”,”addr”:”http:///”}
addResource result is : {“id”:”NO2″,”name”:”Joker”,”addr”:”http://”}
getAllResource result is : [{“id”:”NO2″,”name”:”Joker”,”addr”:”http://”}]

(2)HttpURLConnection

package com.restful.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.restful.entity.PersonEntity;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by XuHui on 2017/8/7.
 */
public class HttpURLClient {
    private static String REST_API = "http://localhost:8080/jerseyDemo/rest/JerseyService";

    public static void main(String[] args) throws Exception {
        addResource();
        getAllResource();
    }

    public static void addResource() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        URL url = new URL(REST_API + "/addResource/person");
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Accept", "application/json");
        httpURLConnection.setRequestProperty("Content-Type", "application/json");
        PersonEntity entity = new PersonEntity("NO2", "Joker", "http://");
        OutputStream outputStream = httpURLConnection.getOutputStream();
        outputStream.write(mapper.writeValueAsBytes(entity));
        outputStream.flush();

        BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
        String output;
        System.out.print("addResource result is : ");
        while ((output = reader.readLine()) != null) {
            System.out.print(output);
        }
        System.out.print("\n");
    }

    public static void getAllResource() throws Exception {
        URL url = new URL(REST_API + "/getAllResource");
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("GET");
        httpURLConnection.setRequestProperty("Accept", "application/json");
        BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
        String output;
        System.out.print("getAllResource result is :");
        while ((output = reader.readLine()) != null) {
            System.out.print(output);
        }
        System.out.print("\n");
    }

}

结果:

addResource result is : {"id":"NO2","name":"Joker","addr":"http://"}
getAllResource result is :[{"id":"NO2","name":"Joker","addr":"http://"}]

(3)HttpClient

package com.restful.client;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.restful.entity.PersonEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

/**
 * Created by XuHui on 2017/8/7.
 */
public class RestfulHttpClient {
    private static String REST_API = "http://localhost:8080/jerseyDemo/rest/JerseyService";

    public static void main(String[] args) throws Exception {
        addResource();
        getAllResource();
    }

    public static void addResource() throws Exception {
        HttpClient httpClient = new DefaultHttpClient();
        PersonEntity entity = new PersonEntity("NO2", "Joker", "http://");
        ObjectMapper mapper = new ObjectMapper();

        HttpPost request = new HttpPost(REST_API + "/addResource/person");
        request.setHeader("Content-Type", "application/json");
        request.setHeader("Accept", "application/json");
        StringEntity requestJson = new StringEntity(mapper.writeValueAsString(entity), "utf-8");
        requestJson.setContentType("application/json");
        request.setEntity(requestJson);
        HttpResponse response = httpClient.execute(request);
        String json = EntityUtils.toString(response.getEntity());
        System.out.print("addResource result is : " + json + "\n");
    }

    public static void getAllResource() throws Exception {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet(REST_API + "/getAllResource");
        request.setHeader("Content-Type", "application/json");
        request.setHeader("Accept", "application/json");
        HttpResponse response = httpClient.execute(request);
        String json = EntityUtils.toString(response.getEntity());
        System.out.print("getAllResource result is : " + json + "\n");
    }
}

结果:

addResource result is : {"id":"NO2","name":"Joker","addr":"http://"}
getAllResource result is : [{"id":"NO2","name":"Joker","addr":"http://"}]

maven:

<dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.1.2</version>
</dependency>

(4)JAX-RS API

package com.restful.client;

import com.restful.entity.PersonEntity;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;

/**
 * Created by XuHui on 2017/7/27.
 */
public class RestfulClient {
    private static String REST_API = "http://localhost:8080/jerseyDemo/rest/JerseyService";
    public static void main(String[] args) throws Exception {
        getRandomResource();
        addResource();
        getAllResource();
    }

    public static void getRandomResource() throws IOException {
        Client client = ClientBuilder.newClient();
        client.property("Content-Type","xml");
        Response response = client.target(REST_API + "/getRandomResource").request().get();
        String str = response.readEntity(String.class);
        System.out.print("getRandomResource result is : " + str + "\n");
    }

    public static void addResource() {
        Client client = ClientBuilder.newClient();
        PersonEntity entity = new PersonEntity("NO2", "Joker", "http://");
        Response response = client.target(REST_API + "/addResource/person").request().buildPost(Entity.entity(entity, MediaType.APPLICATION_JSON)).invoke();
        String str  = response.readEntity(String.class);
        System.out.print("addResource result is : " + str + "\n");
    }

    public static void getAllResource() throws IOException {
        Client client = ClientBuilder.newClient();
        client.property("Content-Type","xml");
        Response response = client.target(REST_API + "/getAllResource").request().get();
        String str = response.readEntity(String.class);
        System.out.print("getAllResource result is : " + str + "\n");

    }
}

结果:

getRandomResource result is : {"id":"NO1","name":"Joker","addr":"http:///"}
addResource result is : {"id":"NO2","name":"Joker","addr":"http://"}
getAllResource result is : [{"id":"NO2","name":"Joker","addr":"http://"}]

(5)webClient

package com.restful.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.restful.entity.PersonEntity;
import org.apache.cxf.jaxrs.client.WebClient;

import javax.ws.rs.core.Response;

/**
 * Created by XuHui on 2017/8/7.
 */
public class RestfulWebClient {
    private static String REST_API = "http://localhost:8080/jerseyDemo/rest/JerseyService";
    public static void main(String[] args) throws Exception {
        addResource();
        getAllResource();
    }

    public static void addResource() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        WebClient client = WebClient.create(REST_API)
                .header("Content-Type", "application/json")
                .header("Accept", "application/json")
                .encoding("UTF-8")
                .acceptEncoding("UTF-8");
        PersonEntity entity = new PersonEntity("NO2", "Joker", "http://");
        Response response = client.path("/addResource/person").post(mapper.writeValueAsString(entity), Response.class);
        String json = response.readEntity(String.class);
        System.out.print("addResource result is : " + json + "\n");
    }

    public static void getAllResource() {
        WebClient client = WebClient.create(REST_API)
                .header("Content-Type", "application/json")
                .header("Accept", "application/json")
                .encoding("UTF-8")
                .acceptEncoding("UTF-8");
        Response response = client.path("/getAllResource").get();
        String json = response.readEntity(String.class);
        System.out.print("getAllResource result is : " + json + "\n");
    }
}

结果:

addResource result is : {"id":"NO2","name":"Joker","addr":"http://"}
getAllResource result is : [{"id":"NO2","name":"Joker","addr":"http://"}

maven:

<dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-bundle-jaxrs</artifactId>
      <version>2.7.0</version>
</dependency>

注:该jar包引入和jersey包引入有冲突,不能在一个工程中同时引用。

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/166502.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • OpenCV-Python实战(17)——人脸识别详解

    OpenCV-Python实战(17)——人脸识别详解随着计算机视觉、机器学习和深度学习的发展,人脸识别已经成为一个热门话题。人脸识别具有广泛的应用前景,包括犯罪预防、智能监视以及社交网络。在本文中,我们介绍OpenCV提供的与人脸识别相关的函数,同时还将探索一些用于人脸识别的深度学习方法,这些方法可以轻松集成到计算机视觉项目中以实现高精度的人脸识别。

  • 全局平均池化(全局池化代替全连接层)

    全局平均池化是在论文NetworkinNetwork中提出的,原文中全局平均池化的作用和优点:思想:对于输出的每一个通道的特征图的所有像素计算一个平均值,经过全局平均池化之后就得到一个维度==类别数的特征向量,然后直接输入到softmax层作用:代替全连接层,可接受任意尺寸的图像优点:1)可以更好的将类别与最后一个卷积层的特征图对应起来(每一个通道对应一种…

  • leetcode链表问题_c++反转链表

    leetcode链表问题_c++反转链表给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。示例 1:输入:head = [1,2,3,4,5], left = 2, right = 4输出:[1,4,3,2,5]示例 2:输入:head = [5], left = 1, right = 1输出:[5] 提示:链表中节点数目为 n1 <= n <= 500-500

  • Windows server 80端口被占用怎么办_sqlserver基本介绍

    Windows server 80端口被占用怎么办_sqlserver基本介绍安装sqlserver导致80端口被占用解决方法系统占用的端口一般都是微软官方的产品占用的。所以这个时候主要考虑到几个服务:SQLServer导致。其中很有可能是SQLServerReportingServices(MSSQLSERVER),它是SQLServer的日志系统。IIS服务。如果你电脑安装了这个,很有可能它在运行着,那么它就占用着80端口当然如果都不…

    2022年10月29日
  • STL之Stringstream字符串流使用总结

    STL之Stringstream字符串流使用总结如果你已习惯了风格的转换,也许你首先会问:为什么要花额外的精力来学习基于的类型转换呢?也许对下面一个简单的例子的回顾能够说服你。假设你想用sprintf()函数将一个变量从int类型转换到字符串类型。为了正确地完成这个任务,你必须确保证目标缓冲区有足够大空间以容纳转换完的字符串。此外,还必须使用正确的格式化符。如果使用了不正确的格式化符,会导致非预知的后果。下面是一个例子:intn

  • 学校机房如何摆脱老师控制_怎么摆脱学校机房老师的控制

    学校机房如何摆脱老师控制_怎么摆脱学校机房老师的控制先要弄懂原理,其实教师电脑能控制你就是通过网线把他的屏幕同步(发送)到你的屏幕,所以就算你的主机在运行的话也会被控制,不过你的主机里面还是在运行自己的东西,不过屏幕显示的界面是教师端的界面罢了。分为

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号