Nithyananth Nithyananth -4 years ago 89
reST (reStructuredText) Question

Unexpected 'S' in Postman on consuming REST Api

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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download