Unexpected 'S' in Postman on consuming REST Api
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.
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, 2022Comments
-
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 almost 7 yearsThat'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 almost 7 yearsHow can I make my response as JSON? @user7790438
-
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 theconsumes
argument in yourRequestMapping
. -
Nithyananth almost 7 yearsI want to return
"JSON"
, so can you give the sample code like how it should be. @nbrooks -
ArifMustafa almost 4 yearsEither remove the produces element or make the response media type to JSON.
-