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":"无法读取文档:无法识别令牌“firstName”:在 [Source: java.io.PushbackInputStream@3f7cf4ca; 处期待“null”、“true”、“false”或 NaN\n;行:1,列:11];嵌套异常是 com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'firstName': was expecting 'null', 'true', 'false' or NaN\n at [Source: java.io.PushbackInputStream@3f7cf4ca; 行:1,列:11]","path":"/lodgers"}"

4

2 回答 2

1

这里需要发送的输入应该是如下格式

{
lodgerId : 1,
identityCardDtoList: [{
                        identityCardId: 11,
                        identityCardTypeDto:{
                            identityCardTypeId: 111,
                            identityCardType: "Node 1.1.1",
                            expiration: 12/12/2015,
                            hasExpirationDate: true
                            }
                       },{
                        identityCardId: 12,
                        identityCardTypeDto:{
                            identityCardTypeId: 112,
                            identityCardType: "Node 1.1.2",
                            expiration: 12/12/2015,
                            hasExpirationDate: true
                            }
                     }] 
}

请让我知道这是否是您正在寻找的

于 2015-07-03T14:09:56.030 回答
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 回答