2017-06-13 2 views
0

SpringBootApplicationで非常に基本的なRESTサービスを作成しました。私は、APIをヒットしようとしたとき、私はこれが私のAPIですUnexpected 'S'REST Apiを消費しているときにPostmanで予期せぬ「S」が発生する

のような応答を取得しています、

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

    } 

は、これは私が郵便配達を通じてAPIを打っています私の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> 

ですヘッダーにはcontent-type:application/jsonとなります。しかし、私は予期せぬ「S」を応答として得ています。私はこのリンクunexpected 's' in response of restful serviceに従ったが、私はその解決策を得られなかった。誰もがこれで私を助けることができますか?

+2

レスポンスがJSON形式ではなく、JSONを生成するように指定しています。つまり、JSONを予期していたのにS文字を見つけたためにエラーが発生しました。 –

+0

JSONとしてレスポンスを作成するにはどうすればよいですか? @ user7790438 – Nithyananth

+0

@Nithyananthあなたの応答はオブジェクトまたはコレクション/配列である必要があり、Springがクラスパス上で見つけたコンバーターによって直列化されます。本当にあなたのレスポンスを 'Success ... 'というテキストにしたいのであれば、それはJSONではなく、あなたの応答タイプを' 'text/plain''に変更することができます。また、あなたのサービスは引数を受け付けていないので、 'RequestMapping'に' consumes'引数を必要としません。 – nbrooks

答えて

2

としてはuser7790438nbrooksの両方でコメントで説明し、問題はあなたの応答はあなたがJSON(Content-Type: application/jsonヘッダを提供することにより)をロードするようにということを教えていても、プレーンテキスト(Success...)になるということです。

public class ProjectStatus { 
    private String status; 

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

    public String getStatus() { 
     return status; 
    } 
} 

今、あなたのRESTコントローラであなたはこのように、ResponseEntity<ProjectStatus>を返す必要があります:JSONを返すように

は、あなたがたとえば、あなたがこのようなProjectStatusクラスを作成することができ、オブジェクトのいくつかの種類を返す必要があります:あなたがポストマン使用する場合

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

これは、次のJSONになります:

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

あなたは好きなオブジェクトをかなり返すことができます。たとえば、作成したプロジェクトを応答に戻すことが挙げられます。


あなたはJSONを必要としない場合は別の方法として、あなたはContent-Type: text/plainヘッダを提供することができます。

関連する問題