Spring Boot响应实体不返回JSON

时间:2016-10-08 16:02:21

标签: java json spring spring-boot

当我从RestController返回我的POJO时,它会在JSON对象中进行转换。 但是当我试图返回new ResponseEntity<>("success", HttpStatus.CREATED);时 controller返回纯文本。怎么了?

我的RestController:

    @RequestMapping(value = "/register", method = RequestMethod.POST,consumes = "application/json", produces="application/json")
public ResponseEntity<?>  registerUser(@RequestBody User user) throws MessagingException {
    if(registrationService.canRegister(user.getEmail())){
        registrationService.registerUser(user);
        logger.info("Registered user "+user.getEmail());
        return new ResponseEntity<>("success", HttpStatus.CREATED);
    }
    return new ResponseEntity<>(HttpStatus.CONFLICT);
}

我的依赖项:

    <dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-java8</artifactId>
        <version>5.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-jwt</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.7.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-oauth2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Spring Boot 1.4.1版

2 个答案:

答案 0 :(得分:4)

"success"作为String不能转换为JSON格式。因此,spring控制器返回plain / text。通常,对于要转换为JSON的任何对象,它必须采用键值对的格式,因此所有POJO都可以转换为key的相应键值对是属性名称,value是属性的值。您可以查看此页面以验证构成有效json的内容。 https://jsonformatter.curiousconcept.com/

答案 1 :(得分:0)

刚遇到同样的问题并找到了解决问题的好方法:

KeyValuePair value = new KeyValuePair("additionalAdress", user.getAdditionalEmail());

return new ResponseEntity<>(value, HttpStatus.OK);

在前端(在我的例子中是Angular 2 / Typescript),您可以通过以下方式获得值:

this.userService.updateAdditionalEmailOfCurrUser(this.additionalEmail).subscribe( (res: any) => {
        // handle success case here
        this.additionalEmail = res.value;               
    },
    () => {
        // handle error case here
    });