Searching...
Sunday, 7 February 2016

Spring @RestController

19:01

Spring @RestController

Spring 4.0 introduced @RestController, a specialized version of the controller which is a convenience annotation that does nothing more than add the @Controller and @ResponseBody annotations. By annotating the controller class with @RestController annotation, you no longer need to add @ResponseBody to all the request mapping methods.

Example

package com.vinod;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RestControllerExample {
    @RequestMapping("/hello/{name}")
    public Customer message(@PathVariable String name) {
        return new Customer("Hello " + name, "USA");
    }

}

class Customer {
    private String name;
    private String address;

    public Customer(String name, String address) {
        super();
        this.name = name;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}

 

Download Complete example

https://github.com/kkvinodkumaran/myrepository/tree/master/vinod-spring-webservice

 

 

 

0 comments: