开发者

How to Insert JSON data object to Spring MVC application (via controller) with Jquery

开发者 https://www.devze.com 2023-04-09 09:21 出处:网络
I want to Insert JSON data object (User) to my Spring MVC application (via controller) with Jquery. if code is like follow how can I accomplish this? Thanks.

I want to Insert JSON data object (User) to my Spring MVC application (via controller) with Jquery. if code is like follow how can I accomplish this? Thanks.

User Object

public class User {
    private Integer usId;
    private String usName;
    private Boolean usActive;
}

Spring Controller

@Controller
@RequestMapping(value="/user")
public class UserController {

    @Autowired
    UserService userService;

    private Validator validator;

    @RequestMapping(method=RequestMethod.POST)
    public @ResponseBody Map<String, ? extends Object> create(@RequestBody User user, HttpS开发者_JS百科ervletResponse response) {


        Set<ConstraintViolation<User>> failures = validator.validate(user);
        userService.insert(user);

        if (!failures.isEmpty()) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return validationMessages(failures);
        } else {

            return Collections.singletonMap("id", user.getUsId());
        }
    }
}


$.post("/user", { usName: "John", usActive: "true" } );
0

精彩评论

暂无评论...
验证码 换一张
取 消