1

Question

I am using Spring-boot with Thymeleaf template engine, I have a form in a page, it need to update 2 entity but they have identical property, so i mix the 2 entity into 1 complex object, but when the request to my controller, it's happened JSON parsing error, please help me to figure out what happened please.

Controller for Debugging

    @RequestMapping(value = "shop", method = RequestMethod.POST)
    public String createShop(@RequestBody String json , final Locale locale,
            Model model, HttpSession session) {

        ObjectMapper om = new ObjectMapper();
        try {
            om.readValue(json, CreateShopForm.class);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        model.addAttribute(H2S_LAYOUT_PAGE_TITLE, messageSource.getMessage("cms.common.sidebar.profile", null, locale));
        model.addAttribute(H2S_LAYOUT_PAGE_DESC, "建立商家資料完成");
        model.addAttribute(H2S_LAYOUT_CONTENT, "modules/admin/register_success");

        return "layouts/admin";
    }

Complex Object

public class CreateShopForm{
    private Employee employee;
    private Shop shop;
    public Employee getEmployee() {
        return employee;
    }
    public void setEmployee(Employee employee) {
        this.employee = employee;
    }
    public Shop getShop() {
        return shop;
    }
    public void setShop(Shop shop) {
        this.shop = shop;
    }
}

HTML with Thymeleaf tag

<form enctype='application/json' th:action="@{/admin/shop}" method="post" class="form-horizontal"
  role="form" id="shop-profile-form" th:object="${createShopForm}">

  <h2 class="header smaller lighter blue">公司資料:</h2>

<!--   <div class="form-group has-error"> -->
  <div class="form-group">
    <label class="col-sm-2 col-xs-12 control-label no-padding-right">公司登記名稱</label>
    <div class="col-sm-5 col-xs-12">
      <input type="text" class="col-xs-12"
        th:field="*{shop.registrationName}" />
    </div>
<!--     <div class="help-block col-sm-5 col-xs-12 col-sm-reset inline">
      欄位必填!</div> -->
  </div>

  <div class="form-group">
    <label class="col-sm-2 col-xs-12 control-label no-padding-right">公司統一編號</label>
    <div class="col-sm-5 col-xs-12">
      <input type="text" class="col-xs-12" th:field="*{employee.taxNumber}" />
    </div>
  </div>
</form>

Exception

com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'shop': was expecting ('true', 'false' or 'null')
 at [Source: shop.registrationName=&shop.taxNumber=&shop.owner=&shop.phoneNumberArea=&shop.phoneNumber=&shop.county.id=1&shop.address=&shop.twReceiptContact=&shop.twReceiptPhoneNumberArea=&shop.twReceiptPhoneNumber=&shop.twReceiptPhoneNumberExt=&shop.twReceiptCounty.id=1&shop.twReceiptAddress=&shop.billingEmail=&shop.bankCode=74&shop.bankAccount=&shop.bankImageUuid=&employee.displayName=&employee.PhoneNumberArea=&employee.PhoneNumber=&employee.PhoneNumberExt=&employee.mobileNumber=&employee.email=&employee.password=&shop.displayName=&shop.urlPathname=&_csrf=5692d99c-ea8a-446d-8f4b-ebb18a12ee7d; line: 1, column: 5]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1419)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:508)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._reportInvalidToken(ReaderBasedJsonParser.java:2300)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleOddValue(ReaderBasedJsonParser.java:1459)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:683)
    at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3105)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3051)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2161)
    at com.h2s.papak.admin.controller.AdminShopController$$EP2BYKWO.createShop(AdminShopController.java:90)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springsource.loaded.ri.ReloadedTypeInvoker$2.invoke(ReloadedTypeInvoker.java:122)
Ken Chiang
  • 11
  • 2
  • How are you sending the json to the controller? – Leandro Carracedo Jan 23 '15 at 13:51
  • Sorry for reply so late,i send the json by the form submit button and enctype by application/json, but when i print out the requestbody string, it gets a formdata format string, is this correct? – Ken Chiang Jan 25 '15 at 14:59
  • That's correct, but if you want to send the json and receive in your controller a CreateShopForm object that's pretty simple, check: http://stackoverflow.com/questions/11611843/spring-and-ajax or https://gerrydevstory.com/2013/08/14/posting-json-to-spring-mvc-controller/ – Leandro Carracedo Jan 26 '15 at 05:16

0 Answers0