We use the SSM environment to build the SSM environment to integrate General Mapper and Pagehelper to illustrate the SPRINGMVC implementation of restful services. Querying Resources
Test:
New Resources
First Test:
This time the show has been added successfully. Let's see if this data exists in the database.
We are re-submitting this time:
Update Resources
By default, the put request is not able to submit the form data, and you need to add a filter in Web. XML to resolve:
<!--solve the problem of put requests not submitting form data-
<filter>
<filter-name>HttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
< /filter>
<filter-mapping>
<filter-name>HttpMethodFilter</filter-name>
< Url-pattern>/*</url-pattern>
</filter-mapping>
Test:
Delete a resource
You need to add a filter in Web. XML to resolve an issue where delete requests cannot submit form data:
<!--convert the
POST request to delete or put
to specify a true request parameter with _method-
<filter>
<filter-name >HiddenHttpMethodFilter</filter-name>
<filter-class> org.springframework.web.filter.hiddenhttpmethodfilter</filter-class>
</filter>
< filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/* </url-pattern>
</filter-mapping>
Test:
Controller Code
Package My.ssm.controller;
Import My.ssm.pojo.User;
Import My.ssm.service.UserService;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.http.HttpStatus;
Import org.springframework.http.ResponseEntity;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.PathVariable;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.bind.annotation.RequestParam;
Import Org.springframework.web.bind.annotation.ResponseBody; @RequestMapping ("New/user") @Controller public class Newusercontroller {@Autowired private userservice Userservi
Ce Query user information based on user ID @RequestMapping (value = "{id}", method = Requestmethod.get) @ResponseBody public responseentit Y<user> Queryuserbyid (@PathVariable ("id") Long ID) {try {User user = This.userService.queryUse
Rbyid (ID); if (null = = user) {//resource does not exist, response 404 return Responseentity.status (httpstatus.not_found). Bo
DY (null);
}//resource present, response to//return Responseentity.status (Httpstatus.ok). body (user);
return Responseentity.ok (user);
} catch (Exception e) {e.printstacktrace ();
} return Responseentity.status (Httpstatus.internal_server_error). body (null);
}/** * Added user * * @param users * @return */@RequestMapping (method = Requestmethod.post) Public responseentity<void> saveuser (user user) {try {Boolean bool = This.userService.save
User (user);
if (bool) {//Added successfully, responds 201 return Responseentity.status (httpstatus.created). build ();
}} catch (Exception e) {e.printstacktrace (); }//New failure, response to return Responseentity.status (httpstatus.internal_server_error). build (); }/** * Update users * @param user * @return */@RequestMapping (method = Requestmethod.put) Publi C responseentity<void> updateUser (user user) {try {Boolean bool = This.userService.updateUser (
user);
if (bool) {//update succeeded, Response 204 return Responseentity.status (httpstatus.no_content). build ();
}} catch (Exception e) {e.printstacktrace ();
}//Added failure to respond to the return Responseentity.status (httpstatus.internal_server_error). build ();
}/** * Delete user * * @param ID * @return */@RequestMapping (method = Requestmethod.delete)
Public responseentity<void> DeleteUser (@RequestParam (value = "id", DefaultValue = "0") Long ID) {try { if (id.longvalue () = = 0) {//No parameters are passed, response status code is Responseentity.status (HTT pstatus.bad_request). build ();
} Boolean bool = This.userService.deleteUser (ID);
if (bool) {//delete succeeded, Response 204 return Responseentity.status (httpstatus.no_content). build ();
}} catch (Exception e) {e.printstacktrace ();
}//delete failed with response to the return Responseentity.status (httpstatus.internal_server_error). build ();
}
}
Service Code
Package my.ssm.service;
Import java.util.List;
Import My.ssm.bean.EasyUIResult;
Import My.ssm.mapper.UserMapper;
Import My.ssm.pojo.User;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Service;
Import Com.github.abel533.entity.Example;
Import Com.github.pagehelper.PageHelper;
Import Com.github.pagehelper.PageInfo;
@Service public class UserService {@Autowired private usermapper usermapper; Public easyuiresult queryuserlist (integer page, integer rows) {//Set paging parameter Pagehelper.startpage (page, rows
);
Query User data Example Example = new Example (user.class); Example.setorderbyclause ("Updated DESC");
Set sorting criteria list<user> users = this.userMapper.selectByExample (example);
Get the information after paging pageinfo<user> PageInfo = new pageinfo<user> (users);
return new Easyuiresult (Pageinfo.gettotal (), pageinfo.getlist ()); } public User QueryuserByid (Long ID) {return this.userMapper.selectByPrimaryKey (ID);
Public Boolean saveuser (user user) {return this.userMapper.insert (user) = = 1;
Public Boolean updateUser (user user) {return this.userMapper.updateByPrimaryKeySelective (user) = = 1;
} public Boolean DeleteUser (Long ID) {return this.userMapper.deleteByPrimaryKey (id) = = 1;
}
}