如何使用 spring rest 调用 URL?
Posted
技术标签:
【中文标题】如何使用 spring rest 调用 URL?【英文标题】:How to call a URL using spring rest? 【发布时间】:2018-02-01 19:20:51 【问题描述】:我正在使用 spring rest 示例代码。我想从我的代码中调用一个 URL。我是新来的休息和春天。这是我的控制器
@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);
有模型类
@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 + "]";
当我调用 url localhost:8080/springrestexample/employees/ 时,会出现一些数据。现在我想修改这段代码。如果我调用上面的 url,我想将页面重定向到另一个 URL,例如 https://cp.lk/index.php/cbs/sms/send?
【问题讨论】:
您可以通过返回 http 302 请求浏览器执行此操作。 @ZakiAnwarHamdani 你能解释一下吗? 而不是 return new ResponseEntity(HttpStatus.NOT_FOUND);,尝试 return "redirect:cp.lk/index.php/cbs/sms/send"; @ZakiAnwarHamdani 返回 url 时我必须使用的方法的数据类型是什么 是HttpStatus = OK
还是NOT_FOUND
时重定向到URL?如果是OK
,您还需要返回EmployeeVO
以及URL,因此需要相应地加载您的Response
。
【参考方案1】:
@RestController
public class URLController
@RequestMapping("/foo")
void urlFoo(HttpServletResponse response) throws IOException
response.sendRedirect("some-url");
【讨论】:
请解释为什么该代码可以解决问题中提出的问题。【参考方案2】:这里有很多解决方案:Redirect to an external URL from controller action in Spring MVC
例如使用 RedirectView
在您的用例中:
@RequestMapping(value = "/employees")
public RedirectView redirect()
return new RedirectView("https://cp.lk/index.php/cbs/sms/send");
【讨论】:
以上是关于如何使用 spring rest 调用 URL?的主要内容,如果未能解决你的问题,请参考以下文章
来自 UI 的 Rest Spring 身份验证调用:在请求正文中传递用户名和密码,而不是在 url 参数中,以使用 Spring Security 进行身份验证
springCloud(10):使用Feign实现声明式REST调用-构造多参数请求
如何通过 REST 控制器使用 Spring (Boot) 重写 URL?