2017-02-03 9 views
1

JSONを2つの文字列で返したいのですが、実装方法はわかりません。ここに私のコードは次のとおりです。Spring MVCが「戻り値の型が見つかりません:class org.json.JSONObject」というレポートはなぜですか?

@PostMapping 
public ResponseEntity<> createUser(@RequestBody User user) { 

    JSONObject responseJson = new JSONObject(); 

    if (userService.userExists(user)) { 

     responseJson.put("status", "User with that username already exists."); 

     return new ResponseEntity<>(responseJson, HttpStatus.BAD_REQUEST); 
    } 

    responseJson.put("status", "User created."); 

    return new ResponseEntity<>(responseJson, HttpStatus.CREATED); 
} 

私は私のpom.xmlでJson » 20160810com.fasterxml.jackson.coreを持っているとジャクソンは自動的に私のJSONに変換しませんなぜ私はまだjava.lang.IllegalArgumentException: No converter found for return value of type: class org.json.JSONObjectを取得していますか?これは標準的なもので、単純なキー:値です。おそらく、jackson.coreのいくつかのクラスを使用して単純なJSONを作成する方が良いかもしれないので、私のプロジェクトでJsonのlibをインクルードする必要はなく、jacksonは自動的にそれらを変換しますか?

答えて

2

を混乱ビット 見つけます。 JSONObjectを作成する代わりに、Jacksonの同等のObjectNodeを作成します。

が、これは、ジャクソンタイプですので、ジャクソンはどのように知っているObjectNode

ObjectNode jsonObject = mapper.createObjectNode(); 
jsonObject.put("status", "User with that username already exists."); 
// don't forget to change return type to support this 
return new ResponseEntity<>(jsonObject, HttpStatus.BAD_REQUEST); 

を作成し、移入するためにそれを使用するあなたはSpring MVCのスタックによって使用さObjectMapper

@Autowired 
private ObjectMapper objectMapper; 

へのアクセス権を持っていると仮定すると、それを直列化する。

JSONObjectをシリアル化する方法がわかりません。以下の説明の一部は私の答えhereから来ています。

基本的に、Spring MVCはHandlerMethodReturnValueHandler実装を使用して、@RequestMapping@PostMapping)の注釈付きメソッドによって返される値を処理します。 ResponseEntityの場合、その実装はHttpEntityMethodProcessorです。

この実装は、HttpMessageConverterインスタンスのコレクションをループし、ResponseEntitybodyをインスタンス化できるかどうかをチェックし、可能であれば使用します。残念ながら

、ジャクソンの HttpMessageConverter実装、 MappingJackson2HttpMessageConverterは、これらのオブジェクトをシリアル化するために ObjectMapperを使用して、クラス内の任意のプロパティを発見することはできませんので ObjectMapperJSONObjectをシリアル化することはできません(すなわち。豆ゲッター)。

ジャクソンのHttpMessageConverterは、デフォルトで登録されている他のすべてのことができません。だからこそ、Spring MVCは「コンバータなし」と報告しています。

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class org.json.JSONObject 

代替ソリューションをStringJSONObject自分をシリアル化し、ResponseEntityにそれを渡すことです。明らかに返品のタイプをStringに変更したいと思うでしょう。この場合、Spring MVCはStringHttpMessageConverterを使用します。ただし、コンテンツタイプを追加しないため、application/jsonコンテンツタイプを指定することをお勧めします。たとえば、

HttpHeaders headers = new HttpHeaders(); 
headers.setContentType(MediaType.APPLICATION_JSON); 
return new ResponseEntity<>(responseJson.toString(), headers, HttpStatus.BAD_REQUEST); 
0

Jacksonorg.json.JSONObjectを知らない場合は、key/valueペアの代わりにJsonNodeを使用してください。コメントの後

更新:

あなたは私にあなたがそれを使用する方法の例を記述してもらえますか?私はそれが2つのJSONの解析ライブラリを使用している理由を私は知らない

@PostMapping 
public ResponseEntity<JsonNode> createUser(@RequestBody User user) { 
     ObjectMapper objectMapper = new ObjectMapper(); 

     Map<String, String> responseJson = new HashMap<>(); 
     if (userService.userExists(user)) { 
      responseJson.put("status", 
        "User with that username already exists."); 
      JsonNode jsonNode = objectMapper.valueToTree(responseJson); 
      return new ResponseEntity<JsonNode>(jsonNode, 
        HttpStatus.BAD_REQUEST); 
     } 
     responseJson.put("status", "User created."); 
     JsonNode jsonNode = objectMapper.valueToTree(responseJson); 
     return new ResponseEntity<>(jsonNode, HttpStatus.CREATED); 
} 
関連する問題