0

スプリングレストを使用しています。

オブジェクトを表示して保存したい。

@RequestMapping(value = "/lodgers", method = RequestMethod.POST)
public LodgerInformation createLodger(@RequestBody @Valid final LodgerInformation lodgerDto) {
    return lodgerService.save(lodgerDto);
}

public class LodgerInformation {
    private long lodgerId;
    private String firstName;
    private String lastName;
    private List<IdentityCardDto> identityCardDtoList;
    ...
}



public class IdentityCardDto {
    private long identityCardId;
    private IdentityCardTypeDto identityCardTypeDto;
    private String value;
    ...
}

public class IdentityCardTypeDto {
    private long identityCardTypeId;
    private String identityCardType;
    private Date expiration;
    private boolean hasExpirationDate=false;
    ...
}

HTML側では、名前に使用する必要がある構造は何ですか? html コンポーネントに値を割り当てたり、その逆のプロセスを容易にするライブラリはありますか?

答えを得る:

"{"timestamp":1436292452811,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"ドキュメントを読み取れませんでした: Unrecognizedトークン 'firstName': [ソース: java.io.PushbackInputStream@3f7cf4ca; で 'null'、'true'、'false'、または NaN\n を予期していました。行: 1、列: 11]; ネストされた例外は com.fasterxml.jackson.core.JsonParseException: 認識されないトークン 'firstName': [ソース: java.io.PushbackInputStream@3f7cf4ca; で 'null'、'true'、'false' または NaN\n を予期していました。行: 1, 列: 11]","パス":"/lodgers"}"

4

2 に答える 2

1

フォームは次のようになります

 <form id="newPersonForm">
      <label for="nameInput">Name: </label>
      <input type="text" name="name" id="nameInput" />
      <br/>
       
      <label for="ageInput">Age: </label>
      <input type="text" name="age" id="ageInput" />
      <br/>
      <input type="submit" value="Save Person" /><br/><br/>
      <div id="personFormResponse" class="green"> </div>
    </form>

jquery 呼び出しコードは次のようになります。

$(document).ready(function() {
// Save Person AJAX Form Submit
      $('#newPersonForm').submit(function(e) {
        // will pass the form data using the jQuery serialize function
        $.post('${pageContext.request.contextPath}/api/person', $(this).serialize(), function(response) {
          $('#personFormResponse').text(response);
        });
      });

    });

コントローラーコード

@Controller
@RequestMapping("api")
public class PersonController {

    PersonService personService;
// handles person form submit
@RequestMapping(value="person", method=RequestMethod.POST)
@ResponseBody
public String savePerson(@RequestBody Person person) {
    personService.save(person);
    return "Saved person: " + person.toString();
}

Person オブジェクトは JSON に変換する必要があります。Spring の HTTP メッセージ コンバーター サポートのおかげで、この変換を手動で行う必要はありません。Jackson 2 はクラスパス上にあるため、Spring の MappingJackson2HttpMessageConverter が自動的に選択されて Greeting インスタンスが JSON に変換されます。コントローラーに送信された Person オブジェクトは、jquery によってマップされ、Person オブジェクトを作成します。@RequestBody

于 2015-07-03T18:51:47.537 に答える