2015年6月7日日曜日

Chromebブラウザで「No 'Access-Control-Allow-Origin' header is present on the requested resource」エラーの解決方法

chromeのブラウザでrest apiを呼んだら、こんなエラーが出た

No 'Access-Control-Allow-Origin' header is present on the requested resource

エラーの解消方法は、
Windowsユーザーの場合は、
こんな感じで起動すれば解決できるはずです。

chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security
他の環境、マックユーザーなどの場合は、
ChromeのプラグインCORSをインストールして、
後そのプラグインの設定のところ、「Enable cross-origin resource sharing 」ONにすれば解決できるはずです。
CORSのプラグインURLは以下の通りです。
https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en-US)

Spring Restfull APIで200 OKレスポンスを返す方法

関数にアノテーション「@ResponseStatus(HttpStatus.OK)」を追加
関数の返却値は「void」で定義する

例:
@RequestMapping(value = “/xxxx/{Id:.+}", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
public void delete(@RequestHeader(“code") final String code, @PathVariable final String Id) throws ApplicationException{
    // 処理
    try {
        
    } catch (ApplicationException e) {
        
        }
    }
}

Spring Data JPAでselect IN-clause クエリーの使い方

Spring Data JPAでselect IN-clause クエリーの使い方


@Query(value = "SELECT * FROM User WHERE id IN (:ids)", nativeQuery = true)
Set<User> findByIdInSet(@Param("ids") Set<Long> ids);