2017-02-01 10 views
-1

私は、ユーザーを表すために、このクラスを持っている:私は何をしようとしている春ブーツHttpMessageNotReadableException

@JsonInclude(JsonInclude.Include.NON_NULL) 
@JsonIgnoreProperties(ignoreUnknown = true) 
@Getter 
@Setter 
public class UserDto implements Serializable { 
    private Long id; 
    private String username; 
    private String email; 
    private String password; 
    private Collection<? extends GrantedAuthority> roles; 

    public UserDto() { 
    } 
} 

}

はUserDtoに以下のコントローラの春マップ@RequestBodyをさせることです。

public class AuthController { 
    @Autowired 
    private UserService userService; 
    @Autowired 
    private AuthService authService; 

    @RequestMapping(value = "signup", method = RequestMethod.POST) 
    public ResponseEntity<?> addUser(@RequestBody UserDto userDto) throws Exception { 
     UserDto existingUserDto; 

     try { 
      existingUserDto = userService.save(userDto); 
     } catch (Exception e) { 
      return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); 
     } 
     return new ResponseEntity<>(existingUserDto, HttpStatus.CREATED); 
    } 
} 

しかし、その代わりに、私はPOSTリクエストを送信しようとすると、私は春から次のエラーを取得:

を3210
Failed to read HTTP message:     org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unrecognized token 'username': was expecting ('true', 'false' or 'null') 
at [Source: [email protected]; line: 1, column: 10]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'username': was expecting ('true', 'false' or 'null') 
at [Source: [email protected]; line: 1, column: 10] 

私は何をしようとしたことは、カスタムMappingJackson2HttpMessageConverterを宣言しましたが、私はここで

@Bean 
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { 
     MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(); 
     ObjectMapper objectMapper = new ObjectMapper(); 
     objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 
     jsonConverter.setObjectMapper(objectMapper); 
     return jsonConverter; 
    } 

要求された私は、コンテンツタイプで(やっているの画面何が違うの結果を取得できませんでした:アプリケーション/ JSONを):

PostMan Request

あなたが任意のアイデアを持っていますか?

+1

リクエスト本体にJSONコンテンツを表示します。 –

+0

JSONではなくフォームデータを送信しています。 '@ RequestBody'を取り除く。これは単純なフォームパラメータでなければなりません。 –

+0

リクエストの画面を追加しました。返信いただきありがとうございます。 – fraccaman

答えて

0

コントローラメソッド@RequestMappingを次のように変更してください。

@RequestMapping(value = "", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }) 

はまた、あなたはあなたのコントローラに注釈を付け、正しい要求のマッピングを持っていることを確認しdone.Alsoこの事を取得するには、カスタムMappingJackson2HttpMessageConverterを追加する必要はありません。

@Controller 
@RequestMapping("/api/users") 

また、モデルクラスで次のアノテーションを削除します。デフォルトのコンストラクタを保持したまま、すべてのプロパティにgetterとsetterを追加します。

@JsonInclude(JsonInclude.Include.NON_NULL) 
@JsonIgnoreProperties(ignoreUnknown = true) 
@Getter 
@Setter 

これが役立ちます。幸せなコーディング!

+0

ご返信ありがとうございます!残念ながら '@ RequestMapping'を変更しても機能しませんでしたが、以前と同じエラーが発生しています。私は '@ RequestMapping'も二重チェックしました。それは私のapplication.proprietiesファイルに' server.contextPath =/api'を宣言して以来です。再度ありがとう:) – fraccaman

+0

回答が更新されました。それを試してみてください。ここに投稿しているJSONは何ですか? –

+0

私もすべての注釈を削除しようとしましたが、何も変化していません。 '@ Getters'と' @ Setter'は[Lombok](https://projectlombok.org/)の機能で、注釈でセッターとゲッターを与えます。あなたの最後の質問であなたが何を参照しているのかわかりませんでした。 – fraccaman