1

When i return my POJO from RestController it's transforms in JSON object. But when i try to return new ResponseEntity<>("success", HttpStatus.CREATED); controller returns plain-text. What's wrong?

My 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);
}

My dependencies:

    <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 version 1.4.1

0

2 Answers 2

5

"success" as a String cannot be cast to JSON format. Hence, spring controller returns plain/text.Generally, for any object to be cast as JSON, it must be in the format of a key value pair, so all POJOs can be converted to corresponding key-value pairs where key is the property name and value is property's value. You can have a look at this page to validate what constitutes a valid json. https://jsonformatter.curiousconcept.com/

Sign up to request clarification or add additional context in comments.

3 Comments

To add to the above answer - if you don't want to create new POJO just to send the success string, you can create a Map and add a key with value as success.
@rolson-quadras thank you that's exactly what I was looking for
0

Just had the same Problem and found a decent way to solve it:

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

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

And on frontend (in my case it's Angular 2 / Typescript) you can get the value the following way:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.