Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation
This project
Loading...
Sign in
platform
/
finance_serv
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit c40c3002
authored
Aug 02, 2018
by
王军业
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
1
1 parent
32d32e5a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
358 additions
and
0 deletions
code/finance_serv/src/main/java/com/vion/financial/config/FinanceConfig.java
code/finance_serv/src/main/java/com/vion/financial/utils/HttpNetClient.java
code/finance_serv/src/main/java/com/vion/financial/utils/JavaBean2Json.java
code/finance_serv/src/main/java/com/vion/financial/config/FinanceConfig.java
0 → 100644
View file @
c40c300
package
com
.
vion
.
financial
.
config
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.stereotype.Component
;
@Component
@ConfigurationProperties
(
prefix
=
"finance"
)
public
class
FinanceConfig
{
private
String
authurl
;
public
String
getAuthurl
()
{
return
authurl
;
}
public
void
setAuthurl
(
String
authurl
)
{
this
.
authurl
=
authurl
;
}
}
code/finance_serv/src/main/java/com/vion/financial/utils/HttpNetClient.java
0 → 100644
View file @
c40c300
package
com
.
vion
.
financial
.
utils
;
import
java.io.IOException
;
import
java.nio.charset.Charset
;
import
java.util.Map
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.http.HttpResponse
;
import
org.apache.http.client.ClientProtocolException
;
import
org.apache.http.client.HttpClient
;
import
org.apache.http.client.methods.HttpDelete
;
import
org.apache.http.client.methods.HttpGet
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.client.methods.HttpPut
;
import
org.apache.http.entity.StringEntity
;
import
org.apache.http.impl.client.DefaultHttpClient
;
import
org.apache.http.util.EntityUtils
;
public
class
HttpNetClient
{
public
static
String
doGet
(
String
url
,
String
atoken
)
{
String
result
=
""
;
HttpResponse
response
;
if
(
url
!=
null
&&
url
.
length
()
!=
0
)
{
HttpGet
httpGet
=
new
HttpGet
(
url
);
httpGet
.
setHeader
(
"Accept-Encoding"
,
"gzip, deflate"
);
httpGet
.
setHeader
(
"Accept-Language"
,
"zh-CN"
);
httpGet
.
setHeader
(
"Accept"
,
"application/json, application/xml, text/html, text/*, image/*, */*"
);
httpGet
.
setHeader
(
"authorization"
,
atoken
);
try
{
response
=
new
DefaultHttpClient
().
execute
(
httpGet
);
if
(
response
!=
null
)
{
int
statusCode
=
response
.
getStatusLine
().
getStatusCode
();
if
(
statusCode
==
200
||
statusCode
==
403
)
{
result
=
EntityUtils
.
toString
(
response
.
getEntity
());
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
return
result
;
}
public
static
String
doPost
(
String
url
,
String
msg
,
String
authorization
)
{
HttpResponse
response
;
String
result
=
""
;
if
(
url
!=
null
&&
url
.
length
()
!=
0
)
{
HttpPost
post
=
new
HttpPost
(
url
);
//post.setHeader("Accept-Encoding", "gzip, deflate");
//post.setHeader("Accept-Language", "zh-CN");
post
.
setHeader
(
"Accept"
,
"application/json, application/xml, text/html, text/*, image/*, */*"
);
post
.
setHeader
(
"Content-type"
,
"application/json; charset=utf-8"
);
if
(
StringUtils
.
isNotBlank
(
authorization
)){
post
.
setHeader
(
"authorization"
,
authorization
);
}
DefaultHttpClient
client
=
new
DefaultHttpClient
();
try
{
StringEntity
se
=
new
StringEntity
(
msg
,
String
.
valueOf
(
Charset
.
forName
(
"utf-8"
)));
se
.
setContentEncoding
(
"UTF-8"
);
se
.
setContentType
(
"application/json"
);
post
.
setEntity
(
se
);
response
=
client
.
execute
(
post
);
System
.
out
.
println
(
"response:"
+
response
);
if
(
response
!=
null
)
{
int
statusCode
=
response
.
getStatusLine
().
getStatusCode
();
//if (statusCode == 200 || statusCode == 403) {
result
=
EntityUtils
.
toString
(
response
.
getEntity
());
//}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
return
result
;
}
public
static
String
doPut
(
String
url
,
String
msg
)
{
String
result
=
""
;
if
(
url
!=
null
&&
url
.
length
()
>
0
)
{
StringBuilder
sb
=
new
StringBuilder
();;
HttpPut
request
=
new
HttpPut
(
url
);
String
sck
=
sb
.
toString
();
if
(
sck
.
length
()
>
0
)
{
request
.
setHeader
(
"Cookie"
,
sck
);
}
request
.
setHeader
(
"Accept-Encoding"
,
"gzip, deflate"
);
request
.
setHeader
(
"Accept-Language"
,
"zh-CN"
);
request
.
setHeader
(
"Accept"
,
"application/json, application/xml, text/html, text/*, image/*, */*"
);
DefaultHttpClient
client
=
new
DefaultHttpClient
();
HttpResponse
response
;
try
{
StringEntity
se
=
new
StringEntity
(
msg
,
String
.
valueOf
(
Charset
.
forName
(
"utf-8"
)));
se
.
setContentEncoding
(
"UTF-8"
);
request
.
setEntity
(
se
);
response
=
client
.
execute
(
request
);
if
(
response
!=
null
)
{
int
statusCode
=
response
.
getStatusLine
().
getStatusCode
();
if
(
statusCode
==
200
||
statusCode
==
403
)
{
result
=
EntityUtils
.
toString
(
response
.
getEntity
());
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
return
result
;
}
public
static
String
doDelete
(
String
url
)
{
HttpResponse
response
;
String
result
=
""
;
if
(
url
!=
null
&&
url
.
length
()
!=
0
)
{
HttpDelete
delete
=
new
HttpDelete
(
url
);
delete
.
setHeader
(
"Accept-Encoding"
,
"gzip, deflate"
);
delete
.
setHeader
(
"Accept-Language"
,
"zh-CN"
);
delete
.
setHeader
(
"Accept"
,
"application/json, application/xml, text/html, text/*, image/*, */*"
);
try
{
response
=
new
DefaultHttpClient
().
execute
(
delete
);
if
(
response
!=
null
)
{
int
statusCode
=
response
.
getStatusLine
().
getStatusCode
();
if
(
statusCode
==
200
||
statusCode
==
403
)
{
result
=
EntityUtils
.
toString
(
response
.
getEntity
());
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
return
result
;
}
public
static
String
getAuthority
(
String
url
,
String
authorization
,
String
msg
)
throws
ClientProtocolException
,
IOException
{
//参数+解决中文乱码问题
StringEntity
entity
=
new
StringEntity
(
msg
,
String
.
valueOf
(
Charset
.
forName
(
"UTF-8"
)));
entity
.
setContentEncoding
(
"UTF-8"
);
entity
.
setContentType
(
"application/json"
);
HttpPost
post
=
new
HttpPost
(
url
);
post
.
setHeader
(
"authorization"
,
authorization
);
post
.
setEntity
(
entity
);
HttpClient
client
=
new
DefaultHttpClient
();
HttpResponse
response
=
client
.
execute
(
post
);
String
result
=
""
;
if
(
response
!=
null
)
{
int
statusCode
=
response
.
getStatusLine
().
getStatusCode
();
result
=
EntityUtils
.
toString
(
response
.
getEntity
());
Map
map
=
JavaBean2Json
.
Json2JavaBean
(
result
,
Map
.
class
);
map
.
put
(
"responseStatusCode"
,
statusCode
);
result
=
JavaBean2Json
.
javaBean2Json
(
map
);
}
return
result
;
}
}
code/finance_serv/src/main/java/com/vion/financial/utils/JavaBean2Json.java
0 → 100644
View file @
c40c300
package
com
.
vion
.
financial
.
utils
;
import
java.beans.BeanInfo
;
import
java.beans.IntrospectionException
;
import
java.beans.Introspector
;
import
java.beans.PropertyDescriptor
;
import
java.lang.reflect.InvocationTargetException
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.Map
;
import
net.sf.json.JSONArray
;
import
net.sf.json.JSONObject
;
import
net.sf.json.JsonConfig
;
import
net.sf.json.util.PropertyFilter
;
import
com.fasterxml.jackson.databind.DeserializationFeature
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
public
class
JavaBean2Json
{
public
static
String
javaBean2Json
(
Object
obj
)
{
return
JSONObject
.
fromObject
(
obj
,
getJsonConfig
()).
toString
();
}
public
static
<
T
>
T
Json2JavaBean
(
String
json
,
Class
<
T
>
clas
)
{
try
{
ObjectMapper
om
=
new
ObjectMapper
();
om
.
configure
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
,
false
);
return
om
.
readValue
(
json
,
clas
);
}
catch
(
Exception
e
)
{
JSONObject
jsb
=
new
JSONObject
();
return
(
T
)
jsb
.
toBean
(
JSONObject
.
fromObject
(
json
),
clas
,
getJsonConfig
());
}
}
/**
* json转List
* @param json
* @param clazz
* @param <T>
* @return
*/
public
static
<
T
>
List
<
T
>
json2List
(
String
json
,
Class
<
T
>
clazz
)
{
if
(
json
.
startsWith
(
"["
)){
//json.indexOf("[")==0
JSONArray
array
=
JSONArray
.
fromObject
(
json
);
List
<
T
>
list
=
JSONArray
.
toList
(
array
,
clazz
);
return
list
;
}
return
null
;
}
/**
* json转Array
* @param json
* @param clazz
* @param <T>
* @return
*/
public
static
<
T
>
T
[]
json2Array
(
String
json
,
Class
<
T
>
clazz
)
{
if
(
json
.
indexOf
(
"["
)==
0
){
JSONArray
array
=
JSONArray
.
fromObject
(
json
);
T
[]
ray
=
(
T
[])
JSONArray
.
toArray
(
array
,
clazz
);
return
ray
;
}
return
null
;
}
private
static
JsonConfig
getJsonConfig
(){
JsonConfig
jsonConfig
=
new
JsonConfig
();
jsonConfig
.
setJsonPropertyFilter
(
new
PropertyFilter
()
{
public
boolean
apply
(
Object
source
/* 属性的拥有者 */
,
String
name
/* 属性名字 */
,
Object
value
/* 属性值 */
)
{
if
(
value
instanceof
List
){
List
<
Object
>
list
=
(
List
<
Object
>)
value
;
if
(
list
==
null
||
list
.
size
()==
0
)
{
return
true
;
}
}
else
if
(
value
instanceof
Map
){
Map
map
=
(
Map
)
value
;
if
(
map
==
null
||
map
.
size
()
==
0
)
{
return
true
;
}
}
return
null
==
value
||
""
.
equals
(
value
);
}
});
return
jsonConfig
;
}
public
static
void
jsonToMap
(
String
json
,
Map
map
)
{
JSONObject
jsonObject
=
JSONObject
.
fromObject
(
json
);
populate
(
jsonObject
,
map
);
}
private
static
void
populate
(
JSONObject
jsonObject
,
Map
map
)
{
for
(
Iterator
iterator
=
jsonObject
.
entrySet
().
iterator
();
iterator
.
hasNext
();)
{
String
entryStr
=
String
.
valueOf
(
iterator
.
next
());
String
key
=
entryStr
.
substring
(
0
,
entryStr
.
indexOf
(
"="
));
String
value
=
entryStr
.
substring
(
entryStr
.
indexOf
(
"="
)
+
1
,
entryStr
.
length
());
if
(
jsonObject
.
get
(
key
).
getClass
().
equals
(
JSONObject
.
class
))
{
HashMap
_map
=
new
HashMap
();
map
.
put
(
key
,
_map
);
populate
(
jsonObject
.
getJSONObject
(
key
),
((
Map
)
(
_map
)));
}
else
if
(
jsonObject
.
get
(
key
).
getClass
().
equals
(
JSONArray
.
class
))
{
ArrayList
list
=
new
ArrayList
();
map
.
put
(
key
,
list
);
populateArray
(
jsonObject
.
getJSONArray
(
key
),
list
);
}
else
{
map
.
put
(
key
,
jsonObject
.
get
(
key
));
}
}
}
private
static
void
populateArray
(
JSONArray
jsonArray
,
List
list
)
{
for
(
int
i
=
0
;
i
<
jsonArray
.
size
();
i
++)
if
(
jsonArray
.
get
(
i
).
getClass
().
equals
(
JSONArray
.
class
))
{
ArrayList
_list
=
new
ArrayList
();
list
.
add
(
_list
);
populateArray
(
jsonArray
.
getJSONArray
(
i
),
_list
);
}
else
if
(
jsonArray
.
get
(
i
).
getClass
().
equals
(
JSONObject
.
class
))
{
HashMap
_map
=
new
HashMap
();
list
.
add
(
_map
);
populate
(
jsonArray
.
getJSONObject
(
i
),
_map
);
}
else
{
list
.
add
(
jsonArray
.
get
(
i
));
}
}
/***
* 判断客户端发过来的信息是否是json格式
*
* @param json
* @return
*/
public
static
boolean
isJson
(
String
json
)
{
try
{
JSONObject
.
fromObject
(
json
);
return
true
;
}
catch
(
Exception
e
)
{
return
false
;
}
}
@SuppressWarnings
(
"rawtypes"
)
public
static
<
T
>
T
convertMap
(
Class
<
T
>
type
,
Map
map
)
throws
IntrospectionException
,
IllegalAccessException
,
InstantiationException
,
InvocationTargetException
{
BeanInfo
beanInfo
=
Introspector
.
getBeanInfo
(
type
);
// 获取类属性
T
obj
=
(
T
)
type
.
newInstance
();
// 创建 JavaBean 对象
// 给 JavaBean 对象的属性赋值
PropertyDescriptor
[]
propertyDescriptors
=
beanInfo
.
getPropertyDescriptors
();
for
(
int
i
=
0
;
i
<
propertyDescriptors
.
length
;
i
++)
{
PropertyDescriptor
descriptor
=
propertyDescriptors
[
i
];
String
propertyName
=
descriptor
.
getName
();
if
(
map
.
containsKey
(
propertyName
))
{
// 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。
try
{
Object
value
=
map
.
get
(
propertyName
);
Object
[]
args
=
new
Object
[
1
];
args
[
0
]
=
value
;
descriptor
.
getWriteMethod
().
invoke
(
obj
,
args
);
}
catch
(
IllegalArgumentException
e
)
{
}
}
}
return
obj
;
}
}
Write
Preview
Markdown
is supported
Attach a file
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 post a comment