2

I am working with a spring rest example code. I want to call an URL from my code. I'm new to rest and spring. This is my controller

 @RestController
public class EmployeeRESTController 
{
    @RequestMapping(value = "/employees")
    public @ResponseBody EmployeeListVO getAllEmployees() 
    {
        EmployeeListVO employees = new EmployeeListVO();

        EmployeeVO empOne = new EmployeeVO(1,"Lokesh","Gupta","howtodoinjava@gmail.com");
        EmployeeVO empTwo = new EmployeeVO(2,"Amit","Singhal","asinghal@yahoo.com");
        EmployeeVO empThree = new EmployeeVO(3,"Kirti","Mishra","kmishra@gmail.com");


        employees.getEmployees().add(empOne);
        employees.getEmployees().add(empTwo);
        employees.getEmployees().add(empThree);

        return employees;
    }

    @RequestMapping(value = "/employees/{id}")
    @ResponseBody
    public ResponseEntity<EmployeeVO> getEmployeeById (@PathVariable("id") int id) 
    {
        if (id <= 3) {
            EmployeeVO employee = new EmployeeVO(1,"Lokesh","Gupta","howtodoinjava@gmail.com");
            return new ResponseEntity<EmployeeVO>(employee, HttpStatus.OK);
        }
        return new ResponseEntity(HttpStatus.NOT_FOUND);
    }
}

There are the model classes

    @XmlRootElement (name="employees")
public class EmployeeListVO
{
    private List<EmployeeVO> employees = new ArrayList<EmployeeVO>();

    public List<EmployeeVO> getEmployees() {
        return employees;
    }

    public void setEmployees(List<EmployeeVO> employees) {
        this.employees = employees;
    }
}

    @XmlRootElement (name = "employee")
@XmlAccessorType(XmlAccessType.NONE)
public class EmployeeVO implements Serializable
{
    private static final long serialVersionUID = 1L;

    @XmlAttribute
    private Integer id;

    @XmlElement
    private String firstName;

    @XmlElement
    private String lastName;

    @XmlElement
    private String email;

    public EmployeeVO(Integer id, String firstName, String lastName, String email) {
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }

    public EmployeeVO(){

    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "EmployeeVO [id=" + id + ", firstName=" + firstName
                + ", lastName=" + lastName + ", email=" + email + "]";
    }
}

When I call the url localhost:8080/springrestexample/employees/ some data is appeared. Now I want to modify this code. If I call above url I want to redirect the page to another URL like https://cp.lk/index.php/cbs/sms/send?

sndu
  • 933
  • 4
  • 14
  • 40
  • You can request browser to do that by returning http 302. – Zaki Anwar Hamdani Aug 24 '17 at 04:04
  • @ZakiAnwarHamdani can you explain little bit ? – sndu Aug 24 '17 at 04:06
  • instead of return new ResponseEntity(HttpStatus.NOT_FOUND);, try return "redirect:https://cp.lk/index.php/cbs/sms/send"; – Zaki Anwar Hamdani Aug 24 '17 at 04:08
  • @ZakiAnwarHamdani what is the data type of the method do i have to use when returning a url – sndu Aug 24 '17 at 04:26
  • Do you want to redirect to the URL when the `HttpStatus = OK` or when `NOT_FOUND`? In case of `OK`, you would then also need to return the `EmployeeVO` along with the URL, so need to load your `Response` accordingly. – Aniket V Aug 24 '17 at 06:24
  • Many solution here : https://stackoverflow.com/questions/17955777/redirect-to-an-external-url-from-controller-action-in-spring-mvc – EMottet Aug 24 '17 at 08:32

2 Answers2

0
@RestController
public class URLController {

  @RequestMapping("/foo")
  void urlFoo(HttpServletResponse response) throws IOException {
    response.sendRedirect("some-url");
  }

}
0

many solution here : Redirect to an external URL from controller action in Spring MVC

For example with RedirectView

in your use case :

@RequestMapping(value = "/employees")
public RedirectView redirect() 
{
    return new RedirectView("https://cp.lk/index.php/cbs/sms/send");
}
EMottet
  • 310
  • 1
  • 3
  • 14