Spring MVC - Summary
Spring MVC
@Controller
@RequestMapping("student")
@RequestMapping(value = "/add" method=RequestMethod.GET)
@RequestMapping(value = "/add" method=RequestMethod.POST)
/student/fetch/100/satya
@RequestMapping(value="/fetch/{sno}/{name}")
public String getInfo(@PathVariable("sno") String sno, (@PathVariable("sno") String n ) {
}
/student/fetch? sno=100&name=satya
@RequestMapping(value="/fetch")
public String getBoth(@RequestParam("id") String id, @RequestParam("name") String n) {
}
//Form Data
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
public String submit( @ModelAttribute("employee") Employee employee ) {
}
WebServices
@Path("/student")
/student/add
@Path("/add")
@GET
@Path("/usa")
@Produces("text/html")
@POST
@Path("/usa")
@Produces("text/html")
@Path("{rollno}/{name}/{address}")
@Produces("text/html")
public Response get(@PathParam("rollno") String rollno,@PathParam("name") String name,
@PathParam("address") String address) { }
students?rollno=1218&name=SATYA KAVETI&address=VIJAYAWADA
@GET
@Produces("text/html")
public Response get (@QueryParam("rollno") String rollno,@QueryParam("name") String name,
@QueryParam("address") String address) { }
//DefaultValue
@GET
@Produces("text/html")
public Response getResultByPassingValue(@DefaultValue("1000") @QueryParam("rollno") String rollno, @DefaultValue("XXXX") @QueryParam("name") String name,
@DefaultValue("XXXX") @QueryParam("address") String address) {
customers;custNo=100;custName=Satya
@GET
@Produces("text/html")
public Response getResultByPassingValue(
@MatrixParam("rollno") String rollno,
@MatrixParam("name") String name,
@MatrixParam("address") String address) {}
//Form
@POST
@Path("/registerStudent")
@Produces("text/html")
public Response getResultByPassingValue(
@FormParam("rollno") String rollno,
@FormParam("name") String name,
@FormParam("address") String address) {}
// HeaderParam
@GET
@Path("/headerparam")
public Response getHeader(
@HeaderParam("user-agent") String userAgent,
@HeaderParam("Accept") String accept,
@HeaderParam("Accept-Encoding") String encoding,
@HeaderParam("Accept-Language") String lang) {
//Context
@Path("Context ")
public Response getHttpheaders(@Context HttpHeaders headers){
String output = "<h1>@@Context Example - HTTP headers</h1>";
output = output+"<br>ALL headers -- "+ headers.getRequestHeaders().toString();
output = output+"<br>All Cookies -- "+ headers.getCookies().values();
return Response.status(200).entity(output).build();
Spring 4
Sprin3 -@RequestMapping(value="/user/create", method=RequestMethod.POST)
Spring4 -
@GetMapping("/students/{sno}")
public ResponseEntity getStudent(@PathVariable("sno") int sno) {
}
@PostMapping(value = "/students")
public ResponseEntity createStudent(@RequestBody Student student) {
}
@DeleteMapping("/students/{sno}")
public ResponseEntity deleteStudent(@PathVariable int sno) {
}
public ResponseEntity<Student> createUser(@RequestBody User user, UriComponentsBuilder ub){
}
PREVIOUSSpring4 MVC REST Service Example
NEXTSpring JEE