java Postman 在使用 REST Api 时出现意外的“S”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44511588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unexpected 'S' in Postman on consuming REST Api
提问by Nithyananth
I wrote very basic REST service in SpringBootApplication. When I tried to hit the api I am getting response like Unexpected 'S'
我在 SpringBootApplication 中编写了非常基本的 REST 服务。当我尝试点击 api 时,我得到了类似的响应Unexpected 'S'
This is my api,
这是我的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,
这是我的 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 servicebut I didnt get any solution from that. Can anyone help me out in this?
我正在通过带有content-type:application/json
标题的邮递员访问 api 。但是我收到了意外的“S”作为回应。我已经按照这个链接意外的 's' 响应宁静的服务,但我没有从中得到任何解决方案。任何人都可以帮我解决这个问题吗?
回答by g00glen00b
As explained in the comments by both user7790438and 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).
正如user7790438和nbrooks的评论中所解释的,问题是您的响应将是纯文本 ( Success...
),即使您告诉它加载 JSON(通过提供Content-Type: application/json
标头)。
To return JSON, you should return some kind of object, for example, you could create a ProjectStatus
class like this:
要返回 JSON,您应该返回某种对象,例如,您可以创建一个ProjectStatus
这样的类:
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:
现在,在您的 REST 控制器中,您应该返回 a ResponseEntity<ProjectStatus>
,如下所示:
return new ResponseEntity<ProjectStatus>(new ProjectStatus("Success..."), HttpStatus.OK);
This will result in the following JSON when you use Postman:
当您使用 Postman 时,这将产生以下 JSON:
{"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.
或者,如果您不需要 JSON,则可以提供Content-Type: text/plain
标头。