RestTemplete

RestTemplete是由spring提供的,可以用来模拟浏览器进行服务调用的封装好的Api,和Apache 的HttpClient功能相同,在分布式系统中可以用来服务之间的调用。

开发步骤:

1.引入jar包

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

2.定义通信数据模型

package com.yzdc.in.model;

import java.io.Serializable;

public class MSG implements Serializable{
    String status;

    Object data;

    public MSG() {
    }

    public MSG(String status) {
        this.status = status;
    }
    
    public MSG(String status, Object data) {
        this.status = status;
        this.data = data;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
    
}

3.编写被调用方的Restful风格的Api

@RequestMapping(value = "message_consumer",method = RequestMethod.POST,consumes="application/json")
    @ResponseBody
    public MSG receiveMessage(@RequestBody ReportData reportData){

        genetatorService.generatorMessage(reportData.getEquipId(),reportData.getThresholdId(),reportData.getValue());

        return new MSG("200");
    }

4.实例化RestTemplete(交给spring)

spring-mvc.xml:

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <mvc:annotation-driven/>

    <!-- 扫描controller(controller层注入)-->
    <context:component-scan base-package="com.yzdc.in.controller"/>

    <mvc:view-controller path="/" view-name="index"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="order" value="1"/>
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    </bean>

    <!-- Client -->
    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
        <property name="messageConverters">
            <list>
                <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>
</beans>

4.调用方使用RestTemplete调用:

注入RestTempete

@Autowired
    private RestTemplate restTemplate;

调用url:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<ReportData> entity = new HttpEntity<ReportData>(reportData,headers);
MSG msg = restTemplate.postForObject("http://localhost:8016/minapp/alarm/generator/message_consumer",entity,MSG.class);
原文地址:https://www.cnblogs.com/inspred/p/10491160.html