Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9328f93

Browse files
修补多值订阅bug
1 parent 520d488 commit 9328f93

File tree

12 files changed

+242
-4
lines changed

12 files changed

+242
-4
lines changed

‎chapter14/Spring Demo/version 1-student/rxjava-web/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@
7474
<version>1.5.0</version>
7575
</dependency>
7676

77+
<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
78+
<dependency>
79+
<groupId>javax.xml.bind</groupId>
80+
<artifactId>jaxb-api</artifactId>
81+
<version>2.3.0</version>
82+
</dependency>
7783
</dependencies>
7884

7985
<build>

‎chapter14/Spring Demo/version 1-student/rxjava-web/src/main/java/com/dockerx/rxjavaweb/RxjavaWebApplication.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.web.client.RestTemplate;
57

68
@SpringBootApplication
79
public class RxjavaWebApplication {
810

11+
@Bean
12+
public RestTemplate restTemplate() {
13+
return new RestTemplate();
14+
}
15+
16+
917
public static void main(String[] args) {
1018
SpringApplication.run(RxjavaWebApplication.class, args);
1119
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.dockerx.rxjavaweb.config;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import springfox.documentation.builders.ApiInfoBuilder;
7+
import springfox.documentation.builders.PathSelectors;
8+
import springfox.documentation.builders.RequestHandlerSelectors;
9+
import springfox.documentation.service.ApiInfo;
10+
import springfox.documentation.spi.DocumentationType;
11+
import springfox.documentation.spring.web.plugins.Docket;
12+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
13+
14+
/**
15+
* @author Author 知秋
16+
* @email fei6751803@163.com
17+
* @time Created by Auser on 2018年3月25日 20:41.
18+
*/
19+
@Configuration
20+
@EnableSwagger2
21+
public class SwaggerConfig {
22+
@Value("${swagger.title}")
23+
private String title;
24+
25+
@Value("${swagger.description}")
26+
private String description;
27+
28+
@Value("${swagger.version}")
29+
private String version;
30+
31+
@Bean
32+
public Docket api() {
33+
return new Docket(DocumentationType.SWAGGER_2)
34+
.apiInfo(apiInfo()).select()
35+
.apis(RequestHandlerSelectors.basePackage("com.dockerx.rxjavaweb")).paths(
36+
PathSelectors.any()).build();
37+
}
38+
39+
/**
40+
* This method will return the API info object to swagger which will in turn
41+
* display the information on the swagger UI.
42+
*
43+
* @return the API information
44+
*/
45+
private ApiInfo apiInfo() {
46+
return new ApiInfoBuilder()
47+
.title(title)
48+
.description(description)
49+
.version(version)
50+
.build();
51+
}
52+
}

‎chapter14/Spring Demo/version 1-student/rxjava-web/src/main/java/com/dockerx/rxjavaweb/config/Webconfig.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.springframework.context.annotation.ComponentScan;
55
import org.springframework.context.annotation.Configuration;
66
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
7+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
78
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
89

910
import java.util.List;
@@ -21,4 +22,15 @@ protected void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> retu
2122
super.addReturnValueHandlers(returnValueHandlers);
2223
returnValueHandlers.add(new ObservableReturnValueHandler());
2324
}
25+
26+
@Override
27+
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
28+
super.addResourceHandlers(registry);
29+
registry.addResourceHandler("swagger-ui.html")
30+
.addResourceLocations("classpath:/META-INF/resources/");
31+
32+
registry.addResourceHandler("/webjars/**")
33+
.addResourceLocations("classpath:/META-INF/resources/webjars/");
34+
35+
}
2436
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.dockerx.rxjavaweb.controller;
2+
3+
import com.dockerx.rxjavaweb.domain.dto.CurrencyRatesDTO;
4+
import com.dockerx.rxjavaweb.service.CurrencyConverterService;
5+
import io.swagger.annotations.ApiImplicitParam;
6+
import io.swagger.annotations.ApiOperation;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.http.MediaType;
11+
import org.springframework.web.bind.annotation.*;
12+
import rx.Observable;
13+
14+
import java.util.Set;
15+
16+
/**
17+
* @author Author 知秋
18+
* @email fei6751803@163.com
19+
* @time Created by Auser on 2018年3月25日 22:32.
20+
*/
21+
@RestController
22+
@RequestMapping("/api/currencyconverter")
23+
public class CurrencyController {
24+
private static final Logger logger = LoggerFactory.getLogger(CurrencyController.class);
25+
26+
@Autowired
27+
private CurrencyConverterService currencyConverterService;
28+
29+
@ApiOperation(value="汇率查询", notes="根据想查的币种来查找与指定基础货币对比币种对应的汇率")
30+
@ApiImplicitParam(name = "symbols", value = "所要查询的汇率的币种集合", required = true)
31+
@GetMapping(value = "/rates",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
32+
public Observable<CurrencyRatesDTO> getCurrencyRates(@RequestParam("symbols") Set<String> symbols,@RequestParam("base")String base) {
33+
logger.debug("Retrieving currency rates.");
34+
return currencyConverterService.getCurrencyRates(symbols,base);
35+
}
36+
37+
}

‎chapter14/Spring Demo/version 1-student/rxjava-web/src/main/java/com/dockerx/rxjavaweb/controller/StudentController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.dockerx.rxjavaweb.domain.dto.StudentDTO;
55
import com.dockerx.rxjavaweb.repository.StudentRepository;
66
import com.mongodb.rx.client.Success;
7+
import io.swagger.annotations.ApiImplicitParam;
8+
import io.swagger.annotations.ApiOperation;
79
import org.slf4j.Logger;
810
import org.slf4j.LoggerFactory;
911
import org.springframework.beans.factory.annotation.Autowired;
@@ -29,11 +31,15 @@ public StudentController(StudentRepository studentRepository) {
2931
this.studentRepository = studentRepository;
3032
}
3133

34+
@ApiOperation(value="学生创建", notes="根据BaseStudentDTO对象创建学生")
35+
@ApiImplicitParam(name = "student", value = "学生详细实体student", required = true, dataType = "BaseStudentDTO")
3236
@PostMapping(value = "",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
3337
public Observable<Success> createStudent(@RequestBody BaseStudentDTO student) {
3438
logger.debug("Creating a new Student.");
3539
return studentRepository.createStudent(student);
3640
}
41+
@ApiOperation(value="学生查找", notes="根据name查找相应学生")
42+
@ApiImplicitParam(name = "name", value = "学生name", required = true, dataType = "String")
3743
@GetMapping(value = "",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
3844
public Observable<StudentDTO> getStudentByName(@RequestParam String name) {
3945
logger.debug("Fetching a new student with the Name: " + name);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.dockerx.rxjavaweb.domain.dto;
2+
3+
import java.util.Map;
4+
5+
/**
6+
* @author Author 知秋
7+
* @email fei6751803@163.com
8+
* @time Created by Auser on 2018年3月25日 22:09.
9+
*/
10+
public class CurrencyRatesDTO {
11+
private String base;
12+
private String date;
13+
private Map<String, Double> rates;
14+
15+
public String getBase() {
16+
return base;
17+
}
18+
19+
public CurrencyRatesDTO setBase(String base) {
20+
this.base = base;
21+
return this;
22+
}
23+
24+
public String getDate() {
25+
return date;
26+
}
27+
28+
public CurrencyRatesDTO setDate(String date) {
29+
this.date = date;
30+
return this;
31+
}
32+
33+
public Map<String, Double> getRates() {
34+
return rates;
35+
}
36+
37+
public CurrencyRatesDTO setRates(Map<String, Double> rates) {
38+
this.rates = rates;
39+
return this;
40+
}
41+
}

‎chapter14/Spring Demo/version 1-student/rxjava-web/src/main/java/com/dockerx/rxjavaweb/repository/StudentDao.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ private Document createStudentDocument(BaseStudentDTO student) {
4747
return new Document(DocumentToStudentTransformer.NAME, student.getName())
4848
.append(DocumentToStudentTransformer.AGE, student.getAge())
4949
.append(DocumentToStudentTransformer.CREDIT, student.getCredit())
50-
5150
.append(DocumentToStudentTransformer.MAJOR, student.getMajor());
5251
}
5352
@Override

‎chapter14/Spring Demo/version 1-student/rxjava-web/src/main/java/com/dockerx/rxjavaweb/returnhandler/ObservableReturnValueHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.springframework.web.method.support.ModelAndViewContainer;
99
import rx.Observable;
1010

11+
import java.util.List;
12+
1113
/**
1214
* @author Author 知秋
1315
* @email fei6751803@163.com
@@ -35,9 +37,9 @@ public void handleReturnValue(Object returnValue, MethodParameter returnType, Mo
3537
.startDeferredResultProcessing(new ObservableAdapter<>(observable), mavContainer);
3638
}
3739

38-
public class ObservableAdapter<T> extends DeferredResult<T> {
40+
public class ObservableAdapter<T> extends DeferredResult<List<T>> {
3941
ObservableAdapter(Observable<T> observable) {
40-
observable.subscribe(this::setResult, this::setErrorResult);
42+
observable.toList().subscribe(this::setResult, this::setErrorResult);
4143
}
4244
}
4345
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.dockerx.rxjavaweb.service;
2+
3+
import com.dockerx.rxjavaweb.domain.dto.CurrencyRatesDTO;
4+
import rx.Observable;
5+
6+
import java.util.Set;
7+
8+
/**
9+
* @author Author 知秋
10+
* @email fei6751803@163.com
11+
* @time Created by Auser on 2018年3月25日 22:10.
12+
*/
13+
14+
public interface CurrencyConverterService {
15+
Observable<CurrencyRatesDTO> getCurrencyRates(Set<String> currencies,String base);
16+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /