Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
wwwanlingxiao
mall
Commits
30bb4cd0
Commit
30bb4cd0
authored
Sep 18, 2018
by
zhh
Browse files
集成restTemplate
parent
e298a170
Changes
4
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
30bb4cd0
...
...
@@ -59,9 +59,11 @@ SpringTask定时任务支持 | ✔
docker容器化部署 | ✔
配置区分生产和测试环境 | ✔
ELK日志收集功能 | ✔
R
estTemplate服务间调用 |
R
abbitMq异步通信 |
R
abbitMq异步通信 | ✔
R
estTemplate服务间调用 | ✔
SpringSecurity权限管理功能 |
集成SpringCloud |
集成SpringCloudSecurity |
### 使用工具
...
...
mall-demo/src/main/java/com/macro/mall/demo/config/RestTemplateConfig.java
0 → 100644
View file @
30bb4cd0
package
com.macro.mall.demo.config
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.client.RestTemplate
;
/**
* RestTemplate的配置
* Created by macro on 2018/9/18.
*/
@Configuration
public
class
RestTemplateConfig
{
@Bean
public
RestTemplate
restTemplate
(){
return
new
RestTemplate
();
}
}
mall-demo/src/main/java/com/macro/mall/demo/controller/RestTemplateDemoController.java
0 → 100644
View file @
30bb4cd0
package
com.macro.mall.demo.controller
;
import
com.macro.mall.demo.dto.CommonResult
;
import
com.macro.mall.model.PmsBrand
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.client.RestTemplate
;
import
org.springframework.web.util.UriComponents
;
import
org.springframework.web.util.UriComponentsBuilder
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* RestTemplate示例Controller
* Created by macro on 2018/9/17.
*/
@Api
(
value
=
"RestTemplateDemoController"
,
description
=
"RestTemplate示例"
)
@Controller
@RequestMapping
(
"/template"
)
public
class
RestTemplateDemoController
{
@Autowired
private
RestTemplate
restTemplate
;
@Value
(
"${host.mall.admin}"
)
private
String
HOST_MALL_ADMIN
;
@ApiOperation
(
"getForEntity url"
)
@RequestMapping
(
value
=
"/get/{id}"
,
method
=
RequestMethod
.
GET
)
@ResponseBody
public
Object
getForEntity
(
@PathVariable
Long
id
)
{
String
url
=
HOST_MALL_ADMIN
+
"/brand/{id}"
;
ResponseEntity
<
CommonResult
>
responseEntity
=
restTemplate
.
getForEntity
(
url
,
CommonResult
.
class
,
id
);
return
responseEntity
.
getBody
();
}
@ApiOperation
(
"getForEntity params"
)
@RequestMapping
(
value
=
"/get2/{id}"
,
method
=
RequestMethod
.
GET
)
@ResponseBody
public
Object
getForEntity2
(
@PathVariable
Long
id
)
{
String
url
=
HOST_MALL_ADMIN
+
"/brand/{id}"
;
Map
<
String
,
String
>
params
=
new
HashMap
<>();
params
.
put
(
"id"
,
String
.
valueOf
(
id
));
ResponseEntity
<
CommonResult
>
responseEntity
=
restTemplate
.
getForEntity
(
url
,
CommonResult
.
class
,
params
);
return
responseEntity
.
getBody
();
}
@ApiOperation
(
"getForEntity Uri"
)
@RequestMapping
(
value
=
"/get3/{id}"
,
method
=
RequestMethod
.
GET
)
@ResponseBody
public
Object
getForEntity3
(
@PathVariable
Long
id
)
{
String
url
=
HOST_MALL_ADMIN
+
"/brand/{id}"
;
UriComponents
uriComponents
=
UriComponentsBuilder
.
fromUriString
(
url
).
build
().
expand
(
id
).
encode
();
ResponseEntity
<
CommonResult
>
responseEntity
=
restTemplate
.
getForEntity
(
uriComponents
.
toUri
(),
CommonResult
.
class
);
return
responseEntity
.
getBody
();
}
@ApiOperation
(
"getForObject url"
)
@RequestMapping
(
value
=
"/get4/{id}"
,
method
=
RequestMethod
.
GET
)
@ResponseBody
public
Object
getForObject
(
@PathVariable
Long
id
)
{
String
url
=
HOST_MALL_ADMIN
+
"/brand/{id}"
;
CommonResult
commonResult
=
restTemplate
.
getForObject
(
url
,
CommonResult
.
class
,
id
);
return
commonResult
;
}
@ApiOperation
(
"postForEntity jsonBody"
)
@RequestMapping
(
value
=
"/post"
,
method
=
RequestMethod
.
POST
)
@ResponseBody
public
Object
postForEntity
(
@RequestBody
PmsBrand
brand
)
{
String
url
=
HOST_MALL_ADMIN
+
"/brand/create"
;
ResponseEntity
<
CommonResult
>
responseEntity
=
restTemplate
.
postForEntity
(
url
,
brand
,
CommonResult
.
class
);
return
responseEntity
.
getBody
();
}
@ApiOperation
(
"postForEntity jsonBody"
)
@RequestMapping
(
value
=
"/post2"
,
method
=
RequestMethod
.
POST
)
@ResponseBody
public
Object
postForObject
(
@RequestBody
PmsBrand
brand
)
{
String
url
=
HOST_MALL_ADMIN
+
"/brand/create"
;
CommonResult
commonResult
=
restTemplate
.
postForObject
(
url
,
brand
,
CommonResult
.
class
);
return
commonResult
;
}
@ApiOperation
(
"postForEntity form"
)
@RequestMapping
(
value
=
"/post3"
,
method
=
RequestMethod
.
POST
)
@ResponseBody
public
Object
postForEntity3
(
@RequestParam
String
name
)
{
String
url
=
HOST_MALL_ADMIN
+
"/productAttribute/category/create"
;
//设置头信息
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
MediaType
.
APPLICATION_FORM_URLENCODED
);
//构造表单参数
MultiValueMap
<
String
,
String
>
params
=
new
LinkedMultiValueMap
<>();
params
.
add
(
"name"
,
name
);
HttpEntity
<
MultiValueMap
<
String
,
String
>>
requestEntity
=
new
HttpEntity
<>(
params
,
headers
);
ResponseEntity
<
CommonResult
>
responseEntity
=
restTemplate
.
postForEntity
(
url
,
requestEntity
,
CommonResult
.
class
);
return
responseEntity
.
getBody
();
}
}
mall-demo/src/main/resources/application.properties
View file @
30bb4cd0
#===server start===
server.port
=
8082
#===server end===
#数据库连接池配置
spring.datasource.url
=
jdbc:mysql://localhost:3306/mall
spring.datasource.username
=
root
...
...
@@ -21,4 +25,8 @@ spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type
=
text/html
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache
=
false
#thymeleaf
end
\ No newline at end of file
#thymeleaf end
#host 配置 start
host.mall.admin
=
http://localhost:8080
#host
配置
end
\ No newline at end of file
Write
Preview
Supports
Markdown
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