zoukankan      html  css  js  c++  java
  • Spring 使用 RestTemplate 模拟 电商网站+支付平台

    一、电商网站+支付平台的支付过程

    通过RestTemplate模板对象发送http请求并接收返回结果。

    支付架构:

    支付过程:

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>SSM</groupId>
        <artifactId>SSM</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
        <build>
            <sourceDirectory>src</sourceDirectory>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.0.0</version>
                    <configuration>
                        <warSourceDirectory>WebContent</warSourceDirectory>
                    </configuration>
                </plugin>
            </plugins>
        </build>
        <dependencies>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.3.1</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.1.9.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-dbcp2</artifactId>
                <version>2.1.1</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.1.9.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.3.1</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>4.1.9.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>4.1.9.RELEASE</version>
            </dependency>
            <!-- 对json数据的支持jar -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.8.10</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.8.10</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>2.8.10</version>
            </dependency>
            <!-- 签名加密依赖 -->
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
                <version>1.11</version>
            </dependency>
            <!-- springmvc对xml数据封装的支持 -->
            <dependency>
                <groupId>com.fasterxml.jackson.dataformat</groupId>
                <artifactId>jackson-dataformat-xml</artifactId>
                <version>2.9.0</version>
            </dependency>
            <dependency>
                <groupId>org.codehaus.woodstox</groupId>
                <artifactId>woodstox-core-asl</artifactId>
                <version>4.4.1</version>
            </dependency>
    <!--         spring密码安全机制 -->
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-core</artifactId>
                <version>4.2.0.RELEASE</version>
            </dependency>
        </dependencies>
    </project>

      submit.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <!DOCTYPE html>
    <html>
    <head>
    <base href="<%=basePath%>"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <form action="wenxinpay" method="post">
            <p>订单详情</p>
            <p>苹果X一台,总金额:8888.88</p>
            <p>选择支付方式:微信支付,支付宝支付</p>
            <input type="submit" value="支付">
        </form>
    </body>
    </html>

      OrderController.java

    package com.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.client.RestTemplate;
    
    import com.pojo.OrderInfo;
    import com.pojo.ResultBean;
    
    @Controller
    public class OrderController {
        @Autowired
        RestTemplate rest;//spring发送请求并接受相应对象的模板对象,需要配置一个bean对象
        
        public void setRest(RestTemplate rest) {
            this.rest = rest;
        }
    
        //商户下单
        @RequestMapping(value="/wenxinpay",method=RequestMethod.POST)
        public String weinxinPay(){
            //拿到页面的订单数据
            
            //封装好数据,发送到平台的下单api
            OrderInfo order = new OrderInfo("010101","iphoneX256G全网通5.1金色一台","9895729387239","888888","233.54.23.11","http://localhost:8888/SSM/reback","iyuddgsdhksa","328y4h32jhi7yf348");
            ResultBean rb = rest.postForObject("http://localhost:8888/SSM/testrest", order,ResultBean.class);
            System.out.println("商户拿到平台的返回信息:");
            System.out.println(rb.toString());
            return "redirect:"+rb.getPayUrl();
        }
        
        //平台方拿到订单,并且返回结果
        @RequestMapping(value="/testrest",method=RequestMethod.POST)
        public @ResponseBody ResultBean testRest(@RequestBody OrderInfo order){
            System.out.println("平台从商户拿到的订单信息:");
            System.out.println(order.toString());
            ResultBean rb = new ResultBean("SUCCESS", "成功", "wx731j3uy8u12u8y3", "213892173987", "http://www.baidu.com?id=1123213", "asdu2323g4jygsd", "hdsgjg12323");
            return rb;
        }
    }

      spring-mvc.xml

        <bean id="restTemplate"     class="org.springframework.web.client.RestTemplate">        
  • 相关阅读:
    第十二周 张文小组学习情况总结
    第五章
    第四章
    第三章
    软工实训--学习回顾2
    软工实训-- 学习回顾1
    构建之法——读书笔记(9)
    构建之法——读书笔记(8)
    构建之法——读书笔记(7)
    第13周张文小组学习情况总结
  • 原文地址:https://www.cnblogs.com/wlxslsb/p/10812979.html
Copyright © 2011-2022 走看看