@SuppressWarnings("unchecked") @RequestMapping( value = "/accountancy/update/{id}", method = RequestMethod.PUT, produces = {"application/json; charset=UTF-8"}) public String updateAccountancy(@PathVariable("id") int id, @RequestBody String accountancyStr) { JSONObject result = new JSONObject(); try { JSONObject accountancyObj = (JSONObject) new JSONParser().parse(accountancyStr); AccountancyEntity accountancyEntity = accountancyService.findAccountancyById(id); if (accountancyEntity != null) { accountancyEntity.setAccName( null != accountancyObj.get("accName") ? accountancyObj.get("accName").toString() : null); accountancyEntity.setAccContact( null != accountancyObj.get("accContact") ? accountancyObj.get("accContact").toString() : null); accountancyEntity.setAccPhone( null != accountancyObj.get("accPhone") ? accountancyObj.get("accPhone").toString() : null); accountancyEntity.setAccAddress( null != accountancyObj.get("accAddress") ? accountancyObj.get("accAddress").toString() : null); accountancyService.update(accountancyEntity); result.put("status", "success"); result.put("message", "success"); } else { result.put("status", "error"); result.put("message", "更新有誤 請重新操作"); } } catch (Exception e) { e.printStackTrace(); result.put("status", "error"); result.put("message", "update error"); } return result.toJSONString(); }
// test @SuppressWarnings("unchecked") @RequestMapping( value = "/new/accountancy/add/{id}", method = RequestMethod.POST, produces = {"application/json; charset=UTF-8"}) public String createAccountancy( @PathVariable("id") long id, @RequestBody String newAccountancyStr) { // 參考createCompany那隻 JSONObject result = new JSONObject(); try { JSONObject obj = (JSONObject) new JSONParser().parse(newAccountancyStr); JSONObject accountancyObj = (JSONObject) obj.get("accountancy"); AccountancyEntity Accountancyentity = new AccountancyEntity(); Accountancyentity.setAccName( null != accountancyObj.get("accName") ? accountancyObj.get("accName").toString() : null); Accountancyentity.setAccContact( null != accountancyObj.get("accContact") ? accountancyObj.get("accContact").toString() : null); Accountancyentity.setAccPhone( null != accountancyObj.get("accPhone") ? accountancyObj.get("accPhone").toString() : null); Accountancyentity.setAccAddress( null != accountancyObj.get("accAddress") ? accountancyObj.get("accAddress").toString() : null); accountancyService.ins(Accountancyentity); result.put("status", "success"); result.put("message", "success"); } catch (Exception e) { result.put("status", "error"); result.put("message", "insert error"); } return result.toJSONString(); }