Sunday 6 September 2015

@RequestBody, @ResponseBody, @RequestHeader and @CookieValue

@RequestBody
org.springframework.web.bind.annotation.RequestBody

@Target(value={PARAMETER})
@Retention(value=RUNTIME)

Annotation indicating a method parameter should be bound to the body of the web request.

The body of the request is passed through an HttpMessageConverter to resolve the method argument depending on the content type of the request.

Optionally, automatic validation can be applied by annotating the argument with @Valid.

Supported for annotated handler methods in Servlet environments.

@ResponseBody
org.springframework.web.bind.annotation.ResponseBody

@Target(value={METHOD, TYPE})
@Retention(value=RUNTIME)

Annotation that indicates a method return value should be bound to the web response body. Supported for annotated handler methods in Servlet environments.

As of version 4.0 this annotation can also be added on the type level in which case it is inherited and does not need to be added on the method level.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import org.apache.log4j.Logger;
import com.abusecore.model.Count;
import com.abusecore.model.Status;
import com.abusecore.model.Ticket;
import com.abusecore.services.IDataServices;

@Controller
@RequestMapping("/AbuseCore-1")
public class RestController {

       @Autowired
       IDataServices dataServices;

       /** Logger class to display logs. */
       static final Logger logger = Logger.getLogger(RestController.class);


       @RequestMapping(value = "/count-tickets.json", method = RequestMethod.GET)
       public @ResponseBody Count getTicketsCount() {
              Count count = dataServices.getTicketsCount();
              logger.info("total tickets :" + count);
              return count;
       }

       @RequestMapping(value = "/create", method = RequestMethod.POST,
                     consumes = MediaType.APPLICATION_JSON_VALUE)
       public @ResponseBody Status addEmployee(@RequestBody Ticket ticket) {
              try {
                     dataServices.addEntity(ticket);
                     return new Status(1, "Ticket added Successfully !");
              } catch (Exception e) {
                     // e.printStackTrace();
                     return new Status(0, e.toString());
              }
       }
}

@RequestHeader
org.springframework.web.bind.annotation.RequestHeader

@Target(value={PARAMETER})
@Retention(value=RUNTIME)

Annotation which indicates that a method parameter should be bound to a web request header.

Supported for annotated handler methods in Servlet and Portlet environments.

If the method parameter is Map<String, String> or MultiValueMap<String, String>, or HttpHeaders then the map is populated with all header names and values.

@CookieValue
org.springframework.web.bind.annotation.CookieValue

@Target(value={PARAMETER})
@Retention(value=RUNTIME)

Annotation which indicates that a method parameter should be bound to an HTTP cookie. Supported for annotated handler methods in Servlet and Portlet environments.

The method parameter may be declared as type javax.servlet.http.Cookie or as cookie value type (String, int, etc).

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...