记得上下班打卡 | git大法好,push需谨慎
Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
liquidnet-bus-v1
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
董敬伟
liquidnet-bus-v1
Commits
a6bc4291
Commit
a6bc4291
authored
Jul 12, 2021
by
anjiabin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
提交微信支付
parent
446d93bc
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
345 additions
and
222 deletions
+345
-222
DragonConstant.java
...com/liquidnet/service/dragon/constant/DragonConstant.java
+1
-1
pom.xml
...dnet-service-dragon/liquidnet-service-dragon-impl/pom.xml
+5
-1
WepayPayRespDto.java
...et/service/dragon/channel/wepay/resp/WepayPayRespDto.java
+60
-0
WepayStrategyAppletImpl.java
.../channel/wepay/strategy/impl/WepayStrategyAppletImpl.java
+57
-0
PayController.java
...om/liquidnet/service/dragon/controller/PayController.java
+3
-152
PayNotifyController.java
...uidnet/service/dragon/controller/PayNotifyController.java
+56
-0
XmlUtil.java
...main/java/com/liquidnet/service/dragon/utils/XmlUtil.java
+163
-0
XmlUtils.java
...ain/java/com/liquidnet/service/dragon/utils/XmlUtils.java
+0
-68
No files found.
liquidnet-bus-api/liquidnet-service-dragon-api/src/main/java/com/liquidnet/service/dragon/constant/DragonConstant.java
View file @
a6bc4291
...
@@ -34,7 +34,7 @@ public class DragonConstant {
...
@@ -34,7 +34,7 @@ public class DragonConstant {
public
enum
DeviceFromEnum
{
public
enum
DeviceFromEnum
{
WEB
(
"web"
,
""
),
WAP
(
"wap"
,
""
)
WEB
(
"web"
,
""
),
WAP
(
"wap"
,
""
)
,
APP
(
"app"
,
""
),
JS
(
"js"
,
""
),
APPLE
(
"apple
"
,
""
);
,
APP
(
"app"
,
""
),
JS
(
"js"
,
""
),
APPLE
T
(
"applet
"
,
""
);
private
String
code
;
private
String
code
;
private
String
message
;
private
String
message
;
DeviceFromEnum
(
String
code
,
String
message
)
{
DeviceFromEnum
(
String
code
,
String
message
)
{
...
...
liquidnet-bus-service/liquidnet-service-dragon/liquidnet-service-dragon-impl/pom.xml
View file @
a6bc4291
...
@@ -35,7 +35,11 @@
...
@@ -35,7 +35,11 @@
<artifactId>
alipay-sdk-java
</artifactId>
<artifactId>
alipay-sdk-java
</artifactId>
<version>
4.15.6.ALL
</version>
<version>
4.15.6.ALL
</version>
</dependency>
</dependency>
<dependency>
<groupId>
com.thoughtworks.xstream
</groupId>
<artifactId>
xstream
</artifactId>
<version>
1.4.9
</version>
</dependency>
</dependencies>
</dependencies>
<build>
<build>
...
...
liquidnet-bus-service/liquidnet-service-dragon/liquidnet-service-dragon-impl/src/main/java/com/liquidnet/service/dragon/channel/wepay/resp/WepayPayRespDto.java
0 → 100644
View file @
a6bc4291
package
com
.
liquidnet
.
service
.
dragon
.
channel
.
wepay
.
resp
;
import
com.liquidnet.service.dragon.utils.XmlUtil
;
import
com.thoughtworks.xstream.annotations.XStreamAlias
;
import
lombok.Data
;
/**
* @author AnJiabin <anjiabin@zhengzai.tv>
* @version V1.0
* @Description: TODO
* @class: WepayPayRespDto
* @Package com.liquidnet.service.dragon.channel.wepay.resp
* @Copyright: LightNet @ Copyright (c) 2021
* @date 2021/7/12 10:40
*/
@Data
@XStreamAlias
(
"xml"
)
public
class
WepayPayRespDto
{
@XStreamAlias
(
"return_code"
)
private
String
returnCode
;
//SUCCESS
@XStreamAlias
(
"return_msg"
)
private
String
returnMsg
;
//OK
@XStreamAlias
(
"result_code"
)
private
String
resultCode
;
//SUCCESS
@XStreamAlias
(
"mch_id"
)
private
String
mchId
;
//1551961491
@XStreamAlias
(
"appid"
)
private
String
appid
;
//wx3498304dda39c5a1
@XStreamAlias
(
"nonce_str"
)
private
String
nonceStr
;
//Y6Czfx4lhuSv0yUD
@XStreamAlias
(
"sign"
)
private
String
sign
;
//97001E77813055D03E3009F67A836D62
@XStreamAlias
(
"prepay_id"
)
private
String
prepayId
;
//wx111130490949693ab00f679072ea730000
@XStreamAlias
(
"trade_type"
)
private
String
tradeType
;
//MWEB
@XStreamAlias
(
"mweb_url"
)
private
String
mwebUrl
;
//https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx111130490949693ab00f679072ea730000&package=2011082926
public
static
void
xmlToBean
(){
String
xmlStr
=
"<xml><return_code><![CDATA[SUCCESS]]></return_code>\n"
+
"<return_msg><![CDATA[OK]]></return_msg>\n"
+
"<result_code><![CDATA[SUCCESS]]></result_code>\n"
+
"<mch_id><![CDATA[1551961491]]></mch_id>\n"
+
"<appid><![CDATA[wx3498304dda39c5a1]]></appid>\n"
+
"<nonce_str><![CDATA[Y6Czfx4lhuSv0yUD]]></nonce_str>\n"
+
"<sign><![CDATA[97001E77813055D03E3009F67A836D62]]></sign>\n"
+
"<prepay_id><![CDATA[wx111130490949693ab00f679072ea730000]]></prepay_id>\n"
+
"<trade_type><![CDATA[MWEB]]></trade_type>\n"
+
"<mweb_url><![CDATA[https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx111130490949693ab00f679072ea730000&package=2011082926]]></mweb_url>\n"
+
"</xml> "
;
WepayPayRespDto
respDto
=
XmlUtil
.
toBean
(
xmlStr
,
WepayPayRespDto
.
class
);
System
.
out
.
println
(
"respDto.getAppid()=="
+
respDto
.
getAppid
());
System
.
out
.
println
(
"respDto.getMwebUrl()==="
+
respDto
.
getMwebUrl
());
}
public
static
void
main
(
String
[]
args
)
{
xmlToBean
();
}
}
liquidnet-bus-service/liquidnet-service-dragon/liquidnet-service-dragon-impl/src/main/java/com/liquidnet/service/dragon/channel/wepay/strategy/impl/WepayStrategyAppletImpl.java
0 → 100644
View file @
a6bc4291
package
com
.
liquidnet
.
service
.
dragon
.
channel
.
wepay
.
strategy
.
impl
;
import
com.liquidnet.commons.lang.util.IDGenerator
;
import
com.liquidnet.service.dragon.channel.wepay.strategy.annotation.StrategyWepayHandler
;
import
com.liquidnet.service.dragon.constant.DragonConstant
;
import
com.liquidnet.service.dragon.dto.DragonPayBaseReqDto
;
import
com.liquidnet.service.dragon.dto.DragonPayBaseRespDto
;
import
com.liquidnet.service.dragon.utils.PayWepayUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Component
;
import
java.math.BigDecimal
;
import
java.util.SortedMap
;
import
java.util.TreeMap
;
/**
* @author AnJiabin <anjiabin@zhengzai.tv>
* @version V1.0
* @Description: TODO
* @class: AlipayStrategyWapImpl
* @Package com.liquidnet.service.dragon.channel.alipay.strategy.impl
* @Copyright: LightNet @ Copyright (c) 2021
* @date 2021/7/10 14:34
*/
@Slf4j
@Component
@StrategyWepayHandler
(
DragonConstant
.
DeviceFromEnum
.
APPLET
)
public
class
WepayStrategyAppletImpl
extends
AbstractWepayStrategy
{
@Override
SortedMap
<
String
,
Object
>
buildRequestParamMap
(
DragonPayBaseReqDto
dragonPayBaseReqDto
)
{
String
nonceStr
=
PayWepayUtils
.
getInstance
().
getNonceStr
();
SortedMap
<
String
,
Object
>
parameters
=
new
TreeMap
<>();
parameters
.
put
(
"mch_id"
,
PayWepayUtils
.
getInstance
().
getMerchantId
());
parameters
.
put
(
"appid"
,
PayWepayUtils
.
getInstance
().
getAppId
());
parameters
.
put
(
"nonce_str"
,
nonceStr
);
parameters
.
put
(
"spbill_create_ip"
,
dragonPayBaseReqDto
.
getClientIp
());
parameters
.
put
(
"total_fee"
,
dragonPayBaseReqDto
.
getPrice
().
multiply
(
BigDecimal
.
valueOf
(
100L
)).
intValue
());
parameters
.
put
(
"body"
,
dragonPayBaseReqDto
.
getName
());
parameters
.
put
(
"detail"
,
dragonPayBaseReqDto
.
getDetail
());
parameters
.
put
(
"out_trade_no"
,
IDGenerator
.
payCode
());
parameters
.
put
(
"time_expire"
,
"20210908103456"
);
parameters
.
put
(
"notify_url"
,
"https://www.baidu.com"
);
parameters
.
put
(
"trade_type"
,
"MWEB"
);
return
parameters
;
}
@Override
DragonPayBaseRespDto
buildResponseDto
()
{
return
null
;
}
@Override
protected
String
getRequestUrl
()
{
return
"https://api.mch.weixin.qq.com/pay/unifiedorder"
;
}
}
liquidnet-bus-service/liquidnet-service-dragon/liquidnet-service-dragon-impl/src/main/java/com/liquidnet/service/dragon/controller/PayController.java
View file @
a6bc4291
...
@@ -27,7 +27,7 @@ import java.math.BigDecimal;
...
@@ -27,7 +27,7 @@ import java.math.BigDecimal;
* @date 2021/7/9 12:29
* @date 2021/7/9 12:29
*/
*/
@RestController
@RestController
@RequestMapping
(
"
dragon
"
)
@RequestMapping
(
"
pay
"
)
public
class
PayController
{
public
class
PayController
{
@Autowired
@Autowired
private
IDragonOrdersService
dragonOrdersService
;
private
IDragonOrdersService
dragonOrdersService
;
...
@@ -36,7 +36,7 @@ public class PayController {
...
@@ -36,7 +36,7 @@ public class PayController {
* 电脑网页支付宝支付
* 电脑网页支付宝支付
* @return
* @return
*/
*/
@PostMapping
(
"/
p
ay"
)
@PostMapping
(
"/
dragonP
ay"
)
@ApiOperation
(
"Dragon支付"
)
@ApiOperation
(
"Dragon支付"
)
@ApiResponse
(
code
=
200
,
message
=
"接口返回对象参数"
)
@ApiResponse
(
code
=
200
,
message
=
"接口返回对象参数"
)
@ApiImplicitParams
({
@ApiImplicitParams
({
...
@@ -50,7 +50,7 @@ public class PayController {
...
@@ -50,7 +50,7 @@ public class PayController {
@ApiImplicitParam
(
type
=
"form"
,
required
=
true
,
dataType
=
"String"
,
name
=
"clientIp"
,
value
=
"客户端ip"
,
example
=
"127.0.0.1"
),
@ApiImplicitParam
(
type
=
"form"
,
required
=
true
,
dataType
=
"String"
,
name
=
"clientIp"
,
value
=
"客户端ip"
,
example
=
"127.0.0.1"
),
@ApiImplicitParam
(
type
=
"form"
,
required
=
true
,
dataType
=
"String"
,
name
=
"notifyUrl"
,
value
=
"通知url"
,
example
=
"pay.zhengzai.tv"
),
@ApiImplicitParam
(
type
=
"form"
,
required
=
true
,
dataType
=
"String"
,
name
=
"notifyUrl"
,
value
=
"通知url"
,
example
=
"pay.zhengzai.tv"
),
})
})
public
ResponseDto
<
DragonPayBaseRespDto
>
webAlip
ay
(
public
ResponseDto
<
DragonPayBaseRespDto
>
dragonP
ay
(
@RequestParam
(
value
=
"payType"
)
@NotNull
(
message
=
"支付类型不能为空"
)
String
payType
,
@RequestParam
(
value
=
"payType"
)
@NotNull
(
message
=
"支付类型不能为空"
)
String
payType
,
@RequestParam
(
value
=
"deviceFrom"
)
@NotNull
(
message
=
"设备来源不能为空"
)
String
deviceFrom
,
@RequestParam
(
value
=
"deviceFrom"
)
@NotNull
(
message
=
"设备来源不能为空"
)
String
deviceFrom
,
@RequestParam
(
value
=
"openId"
)
@NotNull
(
message
=
"微信支付openId不能为空"
)
String
openId
,
@RequestParam
(
value
=
"openId"
)
@NotNull
(
message
=
"微信支付openId不能为空"
)
String
openId
,
...
@@ -80,153 +80,4 @@ public class PayController {
...
@@ -80,153 +80,4 @@ public class PayController {
dragonPayBaseReqDto
.
setNotifyUrl
(
notifyUrl
);
dragonPayBaseReqDto
.
setNotifyUrl
(
notifyUrl
);
return
dragonOrdersService
.
dragonPay
(
dragonPayBaseReqDto
);
return
dragonOrdersService
.
dragonPay
(
dragonPayBaseReqDto
);
}
}
// /**
// * 手机网页支付宝支付
// * @return
// */
// @PostMapping("/wap/alipay")
// @ApiOperation("支付宝wap支付")
// @ApiResponse(code = 200, message = "接口返回对象参数")
// @ApiImplicitParams({
// @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "type", value = "支付类型:TICKET,PRODUCT,COST,MBEANS,LIVE,VIDEO,VIP,CLUB,STRAWBERRY", example = "TICKET"),
// @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "price", value = "支付金额", example = "0.1"),
// @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "name", value = "订单名称", example = "测试订单001"),
// @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "detail", value = "订单描述", example = "测试订单001"),
// @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderCode", value = "订单编号", example = "ORDER0001"),
// @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "clientIp", value = "客户端ip", example = "127.0.0.1"),
// @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "notifyUrl", value = "通知url", example = "pay.zhengzai.tv"),
// })
// public ResponseDto<DragonPayBaseRespDto> wapAlipay(@RequestParam(value = "type") @NotNull(message = "支付类型不能为空") String type,
// @RequestParam(value = "price") @NotNull(message = "支付金额不能为空") BigDecimal price,
// @RequestParam(value = "name") @NotNull(message = "订单名称不能为空") String name,
// @RequestParam(value = "detail") @NotNull(message = "订单描述不能为空") String detail,
// @RequestParam(value = "orderCode") @NotNull(message = "订单编号不能为空") String orderCode,
// @RequestParam(value = "clientIp") @NotNull(message = "客户端ip不能为空") String clientIp,
// @RequestParam(value = "notifyUrl") @NotNull(message = "通知Url不能为空") String notifyUrl,
// @RequestParam(value = "returnUrl",required = false) String returnUrl,
// @RequestParam(value = "quitUrl",required = false) String quitUrl,
// @RequestParam(value = "showUrl",required = false) String showUrl,
// @RequestParam(value = "code",required = false) String code,
// @RequestParam(value = "createDate",required = false) String createDate,
// @RequestParam(value = "expireTime",required = false) String expireTime){
// DragonPayBaseReqDto dragonPayBaseReqDto = new DragonPayBaseReqDto();
// dragonPayBaseReqDto.setType(type);
// dragonPayBaseReqDto.setPrice(price);
// dragonPayBaseReqDto.setName(name);
// dragonPayBaseReqDto.setDetail(detail);
// dragonPayBaseReqDto.setOrderCode(orderCode);
// dragonPayBaseReqDto.setClientIp(clientIp);
// dragonPayBaseReqDto.setNotifyUrl(notifyUrl);
// return dragonOrdersService.alipay(dragonPayBaseReqDto);
// }
//
// /**
// * 电脑网页微信支付
// * @return
// */
// @PostMapping("/web/wepay")
// @ApiOperation("微信web支付")
// @ApiResponse(code = 200, message = "接口返回对象参数")
// @ApiImplicitParams({
// @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "type", value = "支付类型:TICKET,PRODUCT,COST,MBEANS,LIVE,VIDEO,VIP,CLUB,STRAWBERRY", example = "TICKET"),
// @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "price", value = "支付金额", example = "0.1"),
// @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "name", value = "订单名称", example = "测试订单001"),
// @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "detail", value = "订单描述", example = "测试订单001"),
// @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderCode", value = "订单编号", example = "ORDER0001"),
// @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "clientIp", value = "客户端ip", example = "127.0.0.1"),
// @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "notifyUrl", value = "通知url", example = "pay.zhengzai.tv"),
// })
// public ResponseDto<DragonPayBaseRespDto> webWepay(@RequestParam(value = "type") @NotNull(message = "支付类型不能为空") String type,
// @RequestParam(value = "price") @NotNull(message = "支付金额不能为空") BigDecimal price,
// @RequestParam(value = "name") @NotNull(message = "订单名称不能为空") String name,
// @RequestParam(value = "detail") @NotNull(message = "订单描述不能为空") String detail,
// @RequestParam(value = "orderCode") @NotNull(message = "订单编号不能为空") String orderCode,
// @RequestParam(value = "clientIp") @NotNull(message = "客户端ip不能为空") String clientIp,
// @RequestParam(value = "notifyUrl") @NotNull(message = "通知Url不能为空") String notifyUrl,
// @RequestParam(value = "returnUrl",required = false) String returnUrl,
// @RequestParam(value = "quitUrl",required = false) String quitUrl,
// @RequestParam(value = "showUrl",required = false) String showUrl,
// @RequestParam(value = "code",required = false) String code,
// @RequestParam(value = "createDate",required = false) String createDate,
// @RequestParam(value = "expireTime",required = false) String expireTime){
// DragonPayBaseReqDto dragonPayBaseReqDto = new DragonPayBaseReqDto();
// dragonPayBaseReqDto.setType(type);
// dragonPayBaseReqDto.setPrice(price);
// dragonPayBaseReqDto.setName(name);
// dragonPayBaseReqDto.setDetail(detail);
// dragonPayBaseReqDto.setOrderCode(orderCode);
// dragonPayBaseReqDto.setClientIp(clientIp);
// dragonPayBaseReqDto.setNotifyUrl(notifyUrl);
// return dragonOrdersService.wepay(dragonPayBaseReqDto);
// }
//
// /**
// * 手机网页微信支付
// * @return
// */
// @PostMapping("/wap/wepay")
// @ApiOperation("微信wap支付")
// @ApiResponse(code = 200, message = "接口返回对象参数")
// public DragonPayBaseRespDto wapWepay(){
// return null;
// }
//
// /**
// * App内支付宝支付
// * @return
// */
// @PostMapping("/app/alipay")
// @ApiOperation("支付宝app支付")
// @ApiResponse(code = 200, message = "接口返回对象参数")
// public DragonPayBaseRespDto appAlipay(){
// return null;
// }
//
// /**
// * App内微信支付
// * @return
// */
// @PostMapping("/app/wepay")
// @ApiOperation("微信app支付")
// @ApiResponse(code = 200, message = "接口返回对象参数")
// public DragonPayBaseRespDto appWepay(){
// return null;
// }
//
// /**
// * Apple(In-App Purchase) 支付
// * @return
// */
// @PostMapping("/app/iappay")
// @ApiOperation("Apple支付")
// @ApiResponse(code = 200, message = "接口返回对象参数")
// public DragonPayBaseRespDto appIappay(){
// return null;
// }
//
// /**
// * 微信内网页、公众号支付
// * @return
// */
// @PostMapping("/js/wepay")
// @ApiOperation("微信内网页、公众号支付")
// @ApiResponse(code = 200, message = "接口返回对象参数")
// public DragonPayBaseRespDto jsWepay(){
// return null;
// }
//
// /**
// * 微信小程序支付
// * @return
// */
// @PostMapping("/applet/wepay")
// @ApiOperation("微信小程序支付")
// @ApiResponse(code = 200, message = "接口返回对象参数")
// public DragonPayBaseRespDto appletWepay(){
// return null;
// }
}
}
liquidnet-bus-service/liquidnet-service-dragon/liquidnet-service-dragon-impl/src/main/java/com/liquidnet/service/dragon/controller/PayNotifyController.java
0 → 100644
View file @
a6bc4291
package
com
.
liquidnet
.
service
.
dragon
.
controller
;
import
com.liquidnet.service.base.ResponseDto
;
import
com.liquidnet.service.dragon.dto.DragonPayBaseRespDto
;
import
io.swagger.annotations.ApiImplicitParam
;
import
io.swagger.annotations.ApiImplicitParams
;
import
io.swagger.annotations.ApiOperation
;
import
io.swagger.annotations.ApiResponse
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RestController
;
import
javax.validation.constraints.NotNull
;
/**
* @author AnJiabin <anjiabin@zhengzai.tv>
* @version V1.0
* @Description: TODO
* @class: PayNotifyController
* @Package com.liquidnet.service.dragon.controller
* @Copyright: LightNet @ Copyright (c) 2021
* @date 2021/7/11 23:35
*/
@RestController
@RequestMapping
(
"pay"
)
public
class
PayNotifyController
{
/**
* 支付宝支付回调
* @return
*/
@PostMapping
(
"/notify/alipay"
)
@ApiOperation
(
"支付宝支付回调"
)
@ApiResponse
(
code
=
200
,
message
=
"接口返回对象参数"
)
@ApiImplicitParams
({
@ApiImplicitParam
(
type
=
"form"
,
required
=
true
,
dataType
=
"String"
,
name
=
"payType"
,
value
=
"支付类型:alipay,wepay,iappay"
,
example
=
"alipay"
)
})
public
ResponseDto
<
DragonPayBaseRespDto
>
notifyAlipay
(
@RequestParam
(
value
=
"payType"
)
@NotNull
(
message
=
"支付类型不能为空"
)
String
payType
){
return
null
;
}
/**
* 微信支付回调
* @return
*/
@PostMapping
(
"/notify/wepay"
)
@ApiOperation
(
"支付宝支付回调"
)
@ApiResponse
(
code
=
200
,
message
=
"接口返回对象参数"
)
@ApiImplicitParams
({
@ApiImplicitParam
(
type
=
"form"
,
required
=
true
,
dataType
=
"String"
,
name
=
"payType"
,
value
=
"支付类型:alipay,wepay,iappay"
,
example
=
"alipay"
)
})
public
ResponseDto
<
DragonPayBaseRespDto
>
notifyWepay
(
@RequestParam
(
value
=
"payType"
)
@NotNull
(
message
=
"支付类型不能为空"
)
String
payType
){
return
null
;
}
}
liquidnet-bus-service/liquidnet-service-dragon/liquidnet-service-dragon-impl/src/main/java/com/liquidnet/service/dragon/utils/XmlUtil.java
0 → 100644
View file @
a6bc4291
package
com
.
liquidnet
.
service
.
dragon
.
utils
;
/**
* @author AnJiabin <anjiabin@zhengzai.tv>
* @version V1.0
* @Description: TODO
* @class: XmlUtil
* @Package com.liquidnet.service.dragon.utils
* @Copyright: LightNet @ Copyright (c) 2021
* @date 2021/7/12 11:15
*/
import
com.thoughtworks.xstream.XStream
;
import
com.thoughtworks.xstream.io.xml.DomDriver
;
import
lombok.extern.slf4j.Slf4j
;
import
org.dom4j.Document
;
import
org.dom4j.DocumentException
;
import
org.dom4j.Element
;
import
org.dom4j.io.SAXReader
;
import
org.xml.sax.InputSource
;
import
java.io.*
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
/**
* 输出xml和解析xml的工具类
*@ClassName:XmlUtil
*/
@Slf4j
public
class
XmlUtil
{
/**
* java 转换成xml
* @Title: toXml
* @Description: TODO
* @param obj 对象实例
* @return String xml字符串
*/
public
static
String
toXml
(
Object
obj
){
XStream
xstream
=
new
XStream
();
// XStream xstream=new XStream(new DomDriver()); //直接用jaxp dom来解释
// XStream xstream=new XStream(new DomDriver("utf-8")); //指定编码解析器,直接用jaxp dom来解释
////如果没有这句,xml中的根元素会是<包.类名>;或者说:注解根本就没生效,所以的元素名就是类的属性
xstream
.
processAnnotations
(
obj
.
getClass
());
//通过注解方式的,一定要有这句话
return
xstream
.
toXML
(
obj
);
}
/**
* 将传入xml文本转换成Java对象
* @Title: toBean
* @Description: TODO
* @param xmlStr
* @param cls xml对应的class类
* @return T xml对应的class类的实例对象
*
* 调用的方法实例:PersonBean person=XmlUtil.toBean(xmlStr, PersonBean.class);
*/
public
static
<
T
>
T
toBean
(
String
xmlStr
,
Class
<
T
>
cls
){
//注意:不是new Xstream(); 否则报错:java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserFactory
XStream
xstream
=
new
XStream
(
new
DomDriver
());
xstream
.
processAnnotations
(
cls
);
T
obj
=(
T
)
xstream
.
fromXML
(
xmlStr
);
return
obj
;
}
/**
* 写到xml文件中去
* @Title: writeXMLFile
* @Description: TODO
* @param obj 对象
* @param absPath 绝对路径
* @param fileName 文件名
* @return boolean
*/
public
static
boolean
toXMLFile
(
Object
obj
,
String
absPath
,
String
fileName
){
String
strXml
=
toXml
(
obj
);
String
filePath
=
absPath
+
fileName
;
File
file
=
new
File
(
filePath
);
if
(!
file
.
exists
()){
try
{
file
.
createNewFile
();
}
catch
(
IOException
e
)
{
log
.
error
(
"创建{"
+
filePath
+
"}文件失败!!!"
+
e
);
return
false
;
}
}
// end if
OutputStream
ous
=
null
;
try
{
ous
=
new
FileOutputStream
(
file
);
ous
.
write
(
strXml
.
getBytes
());
ous
.
flush
();
}
catch
(
Exception
e1
)
{
log
.
error
(
"写{"
+
filePath
+
"}文件失败!!!"
+
e1
);
return
false
;
}
finally
{
if
(
ous
!=
null
)
try
{
ous
.
close
();
}
catch
(
IOException
e
)
{
log
.
error
(
"写{"
+
filePath
+
"}文件关闭输出流异常!!!"
+
e
);
}
}
return
true
;
}
public
static
Map
<
String
,
Object
>
xmlToMap
(
String
xmlDoc
)
throws
DocumentException
{
// 创建一个新的字符串
StringReader
read
=
new
StringReader
(
xmlDoc
);
// 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
InputSource
source
=
new
InputSource
(
read
);
// 创建一个新的SAXBuilder
SAXReader
saxReader
=
new
SAXReader
();
Map
<
String
,
Object
>
xmlMap
=
new
HashMap
<
String
,
Object
>();
Document
doc
=
saxReader
.
read
(
source
);
// 通过输入源构造一个Document
Element
root
=
doc
.
getRootElement
();
// 取的根元素
List
<
Element
>
elements
=
root
.
elements
();
// 得到根元素所有子元素的集合(根元素的子节点,不包括孙子节点)
for
(
Element
et
:
elements
)
{
xmlMap
.
put
(
et
.
getName
(),
et
.
getText
());
}
return
xmlMap
;
}
// /**
// * 从xml文件读取报文
// * @Title: toBeanFromFile
// * @Description: TODO
// * @param absPath 绝对路径
// * @param fileName 文件名
// * @param cls
// * @throws Exception
// * @return T
// */
// public static <T> T toBeanFromFile(String absPath, String fileName,Class<T> cls) throws Exception{
// String filePath = absPath +fileName;
// InputStream ins = null ;
// try {
// ins = new FileInputStream(new File(filePath ));
// } catch (Exception e) {
// throw new Exception("读{"+ filePath +"}文件失败!", e);
// }
//
// String encode = useEncode(cls);
// XStream xstream=new XStream(new DomDriver(encode));
// xstream.processAnnotations(cls);
// T obj =null;
// try {
// obj = (T)xstream.fromXML(ins);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// throw new Exception("解析{"+ filePath +"}文件失败!",e);
// }
// if(ins != null)
// ins.close();
// return obj;
// }
}
\ No newline at end of file
liquidnet-bus-service/liquidnet-service-dragon/liquidnet-service-dragon-impl/src/main/java/com/liquidnet/service/dragon/utils/XmlUtils.java
deleted
100644 → 0
View file @
446d93bc
package
com
.
liquidnet
.
service
.
dragon
.
utils
;
import
org.dom4j.Document
;
import
org.dom4j.DocumentException
;
import
org.dom4j.Element
;
import
org.dom4j.io.SAXReader
;
import
org.xml.sax.InputSource
;
import
java.io.IOException
;
import
java.io.StringReader
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
public
class
XmlUtils
{
/**
* 将xml string 转化为map
*
* @param xmlDoc
* @return
* @throws IOException
* @throws
*/
@SuppressWarnings
(
"unchecked"
)
/*public static Map<String, Object> xmlToMap(String xmlDoc) throws JDOMException, IOException {
// 创建一个新的字符串
StringReader read = new StringReader(xmlDoc);
// 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
InputSource source = new InputSource(read);
// 创建一个新的SAXBuilder
SAXBuilder sb = new SAXBuilder();
Map<String, Object> xmlMap = new HashMap<String, Object>();
Document doc = sb.build(source); // 通过输入源构造一个Document
Element root = doc.getRootElement(); // 取的根元素
List<Element> cNodes = root.getChildren(); // 得到根元素所有子元素的集合(根元素的子节点,不包括孙子节点)
Element et = null;
for (int i = 0; i < cNodes.size(); i++) {
et = (Element) cNodes.get(i); // 循环依次得到子元素
xmlMap.put(et.getName(), et.getText());
}
return xmlMap;
}*/
public
static
Map
<
String
,
Object
>
xmlToMap
(
String
xmlDoc
)
throws
DocumentException
{
// 创建一个新的字符串
StringReader
read
=
new
StringReader
(
xmlDoc
);
// 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
InputSource
source
=
new
InputSource
(
read
);
// 创建一个新的SAXBuilder
SAXReader
saxReader
=
new
SAXReader
();
Map
<
String
,
Object
>
xmlMap
=
new
HashMap
<
String
,
Object
>();
Document
doc
=
saxReader
.
read
(
source
);
// 通过输入源构造一个Document
Element
root
=
doc
.
getRootElement
();
// 取的根元素
List
<
Element
>
elements
=
root
.
elements
();
// 得到根元素所有子元素的集合(根元素的子节点,不包括孙子节点)
for
(
Element
et
:
elements
)
{
xmlMap
.
put
(
et
.
getName
(),
et
.
getText
());
}
return
xmlMap
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment