Unexpected 'S' in Postman on consuming REST Api

16,717

As explained in the comments by both user7790438 and nbrooks, the issue is that your response will be plain text (Success...), even though you tell it to load JSON (by providing the Content-Type: application/json header).

To return JSON, you should return some kind of object, for example, you could create a ProjectStatus class like this:

public class ProjectStatus {
    private String status;

    public ProjectStatus(String status) {
        this.status = status;
    }

    public String getStatus() {
        return status;
    }
}

Now, in your REST controller you should return a ResponseEntity<ProjectStatus>, like this:

return new ResponseEntity<ProjectStatus>(new ProjectStatus("Success..."), HttpStatus.OK);

This will result in the following JSON when you use Postman:

{"status": "Success..."}

You can pretty much return any object you'd like. An example would be to return the created project in your response.


Alternatively, if you don't need JSON, you could provide the Content-Type: text/plain header.

Share:
16,717
Nithyananth
Author by

Nithyananth

I am a software developer who loves to write highly efficient code to build great products and help businesses succeed with their goals.

Updated on June 24, 2022

Comments

  • Nithyananth
    Nithyananth almost 2 years

    I wrote very basic REST service in SpringBootApplication. When I tried to hit the api I am getting response like Unexpected 'S'

    This is my api,

    @RestController
        @RequestMapping(value="/rally")
        public class CreateProjectApi {
    
            @RequestMapping(value = "/createProject", 
                    produces = { "application/json" }, 
                    consumes = { "application/json" }, 
                    method = RequestMethod.GET)
            public ResponseEntity<String> createProj() {
                String s = "Success...";
                return new ResponseEntity<String>(s, HttpStatus.OK);
            }
    
        }
    

    This is my POM.XML,

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.altimetrik.rally</groupId>
        <artifactId>RallyRestApi</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>RallyRestApi</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.4.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <!-- Setup Spring MVC & REST, use Embedded Tomcat -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>com.rallydev.rest</groupId>
                <artifactId>rally-rest-api</artifactId>
                <version>2.2.1</version>
            </dependency>
    </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    

    I am hitting the api through postman with content-type:application/json in header. But I am getting Unexpected 'S' as response. I have followed this link unexpected 's' in response of restful service but I didnt get any solution from that. Can anyone help me out in this?

    • Fady Saad
      Fady Saad almost 7 years
      That's because your response is not in a JSON format and you specify that you will produce JSON, Error meaning that it is expected { sign because it iexpected JSON but he found S character
    • Nithyananth
      Nithyananth almost 7 years
      How can I make my response as JSON? @user7790438
    • nbrooks
      nbrooks almost 7 years
      @Nithyananth Your response would need to be an object or a collection/array, which would then be serialized by the converter that Spring finds on the classpath. If you really just want your response to be the text Success... then that's not JSON, and you can change your response type to "text/plain". Also, since your service isn't accepting any arguments you don't really need the consumes argument in your RequestMapping.
    • Nithyananth
      Nithyananth almost 7 years
      I want to return "JSON", so can you give the sample code like how it should be. @nbrooks
    • ArifMustafa
      ArifMustafa almost 4 years
      Either remove the produces element or make the response media type to JSON.