ITPub博客

首页 > 数据库 > 数据库开发技术 > SpringCloud @RestController返回值问题

SpringCloud @RestController返回值问题

原创 数据库开发技术 作者:HelloTech技术派 时间:2017-12-08 08:00:00 0 删除 编辑
  • 实现一下加法的Controller进行测试,代码如下:

  1. import com.gemantic.commons.Message;

  2. import com.gemantic.commons.ResultData;

  3. import com.gemantic.springcloud.service.Addition;

  4. import io.swagger.annotations.Api;

  5. import io.swagger.annotations.ApiOperation;

  6. import lombok.extern.slf4j.Slf4j;

  7. import org.springframework.beans.factory.annotation.Autowired;

  8. import org.springframework.http.ResponseEntity;

  9. import org.springframework.web.bind.annotation.GetMapping;

  10. import org.springframework.web.bind.annotation.RequestParam;

  11. import org.springframework.web.bind.annotation.ResponseBody;

  12. import org.springframework.web.bind.annotation.RestController;

  13. import javax.validation.constraints.NotNull;

  14. /**

  15. * @author Yezhiwei

  16. * @date 17/12/4

  17. */

  18. @Slf4j

  19. @RestController

  20. @Api(description = "加法接口")

  21. public class AdditionController {

  22.    @Autowired

  23.    private Addition addition;

  24. //    @GetMapping(value = "/add", produces = {"application/json"})

  25.    @GetMapping(value = "/add")

  26. //    @RequestMapping("/add")

  27.    @ApiOperation(value = "a + b = ?")

  28.    public ResponseEntity<ResultData> add(@RequestParam @NotNull Integer a, @RequestParam @NotNull Integer b) {

  29.        if (log.isInfoEnabled()) {

  30.            log.info("add {} + {}", a, b);

  31.        }

  32.        int result = addition.add(a, b);

  33.        ResultData resultData = ResultData.builder().message(Message.builder().code(0).message(Message.SUCCESS_MESSAGE).build()).build();

  34.        resultData.setData(result);

  35.        return ResponseEntity.ok(resultData);

  36.    }

  37. }

  • pom.xml配置文件

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  2.         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  3.    <modelVersion>4.0.0</modelVersion>

  4.    <parent>

  5.        <groupId>org.springframework.boot</groupId>

  6.        <artifactId>spring-boot-starter-parent</artifactId>

  7.        <version>1.5.4.RELEASE</version>

  8.        <relativePath/>

  9.        <!-- lookup parent from repository -->

  10.    </parent>

  11.    <groupId>com.gemantic.springcloud</groupId>

  12.    <artifactId>springcloud-addition-service</artifactId>

  13.    <version>1.0-SNAPSHOT</version>

  14.    <packaging>jar</packaging>

  15.    <name>springcloud-addition-service</name>

  16.    <url>http://maven.apache.org</url>

  17.    <properties>

  18.        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  19.        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  20.        <java.version>1.7</java.version>

  21.        <springfox.version>2.6.1</springfox.version>

  22.    </properties>

  23.    <dependencies>

  24.        <dependency>

  25.            <groupId>com.gemantic.springcloud</groupId>

  26.            <artifactId>springcloud-interface</artifactId>

  27.            <version>1.0-SNAPSHOT</version>

  28.        </dependency>

  29.        <dependency>

  30.            <groupId>org.springframework.boot</groupId>

  31.            <artifactId>spring-boot-starter</artifactId>

  32.        </dependency>

  33.        <dependency>

  34.            <groupId>org.springframework.boot</groupId>

  35.            <artifactId>spring-boot-starter-jetty</artifactId>

  36.        </dependency>

  37.        <dependency>

  38.            <groupId>org.springframework.boot</groupId>

  39.            <artifactId>spring-boot-starter-web</artifactId>

  40.        </dependency>

  41.        <dependency>

  42.            <groupId>org.springframework.cloud</groupId>

  43.            <artifactId>spring-cloud-starter-eureka-server</artifactId>

  44.        </dependency>

  45.        <!-- swagger -->

  46.        <dependency>

  47.            <groupId>io.springfox</groupId>

  48.            <artifactId>springfox-swagger2</artifactId>

  49.            <version>${springfox.version}</version>

  50.        </dependency>

  51.        <dependency>

  52.            <groupId>io.springfox</groupId>

  53.            <artifactId>springfox-swagger-ui</artifactId>

  54.            <version>${springfox.version}</version>

  55.        </dependency>

  56.        <dependency>

  57.            <groupId>org.projectlombok</groupId>

  58.            <artifactId>lombok</artifactId>

  59.            <version>1.16.14</version>

  60.        </dependency>

  61.    </dependencies>

  62.    <dependencyManagement>

  63.        <dependencies>

  64.            <dependency>

  65.                <groupId>org.springframework.cloud</groupId>

  66.                <artifactId>spring-cloud-dependencies</artifactId>

  67.                <version>Dalston.SR1</version>

  68.                <type>pom</type>

  69.                <scope>import</scope>

  70.            </dependency>

  71.        </dependencies>

  72.    </dependencyManagement>

  73.    <build>

  74.        <plugins>

  75.            <plugin>

  76.                <groupId>org.springframework.boot</groupId>

  77.                <artifactId>spring-boot-maven-plugin</artifactId>

  78.                <configuration>

  79.                    <fork>true</fork>

  80.                    <executable>true</executable>

  81.                </configuration>

  82.            </plugin>

  83.        </plugins>

  84.    </build>

  85. </project>

  • 访问测试

出现了很奇怪的再现:

返回的结果不是json

在接口的后缀增加.json,接口正常返回了json数据

在接口的后缀增加.xml,接口返回了xml数据

  • 思考,为什么会出现这样的情况,怎么与之前不一样了,难道是 spring-boot-starter-parent版本的问题?带着问题去找答案

  1. 修改 spring-boot-starter-parent 版本,测试没有效果

  2. @GetMapping(value="/add",produces={"application/json"}) 增加produces类型说明,可以解决,但是要对每个接口都这样做,太累了。。。

  3. 对比之前的项目(因为之前都是返回的json),最后发现是配置原因

把下面的依赖

  1. <dependency>

  2.    <groupId>org.springframework.cloud</groupId>

  3.    <artifactId>spring-cloud-starter-eureka-server</artifactId>

  4. </dependency>

修改为:

  1. <dependency>

  2.    <groupId>org.springframework.cloud</groupId>

  3.    <artifactId>spring-cloud-starter-eureka</artifactId>

  4. </dependency>

修改之后测试

图一的访问方式能正常返回与图二的结果一致。

再按图三的访问方式访问后,提示404。终于找到根本原因了。

更详细的原因或区别就得看源码了

==马虎==


问题已经发布到 http://www.spring4all.com/上,

http://www.spring4all.com/question/93


原文作者:HelloTech技术派;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2017-12-05

  • 博文量
    181
  • 访问量
    73728