栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot结合hessian进行mock测试

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot结合hessian进行mock测试

Hessian是一种常用的远程调用方式,当第三方应用使用hessian协议提供服务时,就不用常用的http测试工具进行mock测试了,因为hessian协议只能用hessian协议访问。这里记录了hessian如何在Spring Boot里调用,进行mock测试模拟。

1、项目结构 1.1 server

1.2 client

2、pom文件 2.1 hessian server


    4.0.0

    com.example
    performanceserver
    0.0.1-SNAPSHOT
    springboot_demo
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.8.RELEASE
         
    


    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            com.caucho
            hessian
            4.0.38
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        



    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2.2  hessian client


    4.0.0

    com.example
    performanceserver
    0.0.1-SNAPSHOT
    springboot_demo
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.8.RELEASE
         
    


    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            com.caucho
            hessian
            4.0.38
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        



        
            org.slf4j
            slf4j-log4j12
        

        
            org.projectlombok
            lombok
            true
        



    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


 3、application.propertities 3.1 hessian server
server.servlet.context-path=/hessian-server
server.port=8280
3.2 hessian client
server.servlet.context-path=/hessian-client
server.port=8180
4、代码实现 4.1 hessian-interface的统一接口
package springboot_demo.service;

public interface HessianServiceInterface {
    String appleUpload(String methodName, String json);

    String test(String name);
}
 4.2 hessian-service的服务注册 4.2.1.注入service,实现hessian-interface中的接口
package springboot_demo.serviceimpl;

import com.google.gson.Gson;
import org.springframework.stereotype.Service;
import springboot_demo.controller.BatchResponese;
import springboot_demo.service.HessianServiceInterface;

@Service
public class HessianTestService implements HessianServiceInterface {

    @Override
    public String appleUpload(String methodName, String json){
        //        System.out.println(methodName);
//        System.out.println(json);

        String result = null;
        BatchResponese response = new BatchResponese();
        response.setApiErrorMsg("");
        response.setApiResponseID("00017E333AD82D3FEFC7FF06F625B63F");
        response.setApiResultCode("A1000");
        response.setApiResultData("{"Response":{"Result":"1","Note":"SUCCESS","payloadDetails":[{"customResponse":{"SNmetaData":{"serialNumbersDetails":[{"serialNumberStatus":"30","serialNumber":"201065HHHG","siteCode":"QTBG","configCode":"0FM2","countryOfOrigin":"","weekCode":"21-09"}],"totalCountSN":"10","partnerID":"3P151AHUB"}},"seqNo":"0001","statusCode":"0000"}],"controlNumber":"000032376"}}");
        Gson gson = new Gson();
        result = gson.toJson(response);
        System.out.println(result);
        return result;
    }

    @Override
    public String test(String str) {
        System.out.println("接口接收:"+str);
        return "aaa-"+str;
    }
}
4.2.2.注入service的服务发现
package springboot_demo.component;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.remoting.caucho.HessianServiceExporter;
import org.springframework.stereotype.Component;
import springboot_demo.service.HessianServiceInterface;
import springboot_demo.serviceimpl.HessianTestService;


@Component
public class HessianUtil {

    @Autowired
    private HessianTestService hessianTestService;

    //发布服务,此处的name必须以斜杠开头,是客户端的访问地址
    @Bean(name = "/hessian/apple")
    public HessianServiceExporter appleUpload_string_string() {
        //HessianServiceExporter 位于spring-web内
        HessianServiceExporter exporter = new HessianServiceExporter();
        //hessianTest已由Spring注入为hessianTestImpl(实现类)对象
        exporter.setService(hessianTestService);
        //注意:此处的HessianTest是接口的类类型,不是实现类的
        exporter.setServiceInterface(HessianServiceInterface.class);
        return exporter;
    }
}
4.3.hessian-client的客户端发现服务
package springboot_demo.component;

import org.springframework.context.annotation.Bean;
import org.springframework.remoting.caucho.HessianProxyFactoryBean;
import org.springframework.stereotype.Component;
import springboot_demo.service.HessianServiceInterface;

@Component
public class HessianClientComponent {

    @Bean
    public HessianProxyFactoryBean helloClient() {
        HessianProxyFactoryBean factory = new HessianProxyFactoryBean();
        //该接口的路径,需要和4.2.2中注册的bean名称一致
        factory.setServiceUrl("http://localhost:8280/hessian-server/hessian/apple");
        //HessianServiceInterface为接口名
        factory.setServiceInterface(HessianServiceInterface.class);
        return factory;
    }
}
4.4 服务端调用测试
package springboot_demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springboot_demo.service.HessianServiceInterface;

@RestController
public class TestController {
    @Autowired
    private HessianServiceInterface hessianService;

    @RequestMapping(value="/test/{id}")
    public String test(@PathVariable("id")String id) {
        String sayTest = hessianService.test("aa"+id);
        System.out.println("请求ID:"+id+  ",接口返回:..............."+sayTest);

        return sayTest;
    }

    @RequestMapping(value = "/apple", method = RequestMethod.GET)
    public String apple(){
        String res = hessianService.appleUpload("","");
        System.out.println(res);
        return res;
    }

}
 4.5 客户端调用测试
package springboot_demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springboot_demo.service.HessianServiceInterface;

@RestController
public class TestController {
    //客户端需要引入相应的接口,客户端调用服务端代码
    @Autowired
    private HessianServiceInterface hessianService;

    @RequestMapping(value="/test/{id}")
    public String test(@PathVariable("id")String id) {
        String sayTest = hessianService.test("aa"+id);
        System.out.println("请求ID:"+id+  ",服务端返回:..............."+sayTest);
        return sayTest;
    }

    @RequestMapping(value = "/apple", method = RequestMethod.GET)
    public String apple(){
        String res = hessianService.appleUpload("","");
        System.out.println(res);
        return res;
    }
}
5.服务启动
5.1 服务端启动
package springboot_demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;


@SpringBootApplication
@ComponentScan("springboot_demo")
public class HessianServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(HessianServerApplication.class, args);
    }
}
5.2 客户端启动
package springboot_demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("springboot_demo")
public class HessianClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(HessianClientApplication.class, args);
    }
}
6 测试

服务端、客户端均调用成功,至此模拟完成,不用再等客户联调了,自己构造数据先行测试。

http://100.118.36.37:8180//hessian-client/apple

 http://100.118.36.37:8280//hessian-server/apple

7 脚本编写过程中遇到的报错 7.1 ComponentScan配置

Field hessianTestService in springboot_demo.component.HessianUtil required a bean of type 'springboot_demo.serviceimpl.HessianTestService' that could not be found.
 

@SpringBootApplication
@ComponentScan("springboot_demo.component")
public class HessianServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(HessianServerApplication.class, args);
    }
}

ComponentScan("springboot_demo.component")扫描的包路径为springboot_demo.component,而服务暴露调用的HessianTestService在springboot_demo.serviceimpl路径下

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/731805.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号