如何将Cross Origin资源共享与Spring MVC 4.0.0 RESTful Webservice集成
我有一个简单的Web服务返回JSON数据。
用户类 ( com.bargadss.SpringService.Domain )是包含的POJO类
user_ID,firstName,lastName,eMail
UserService类 ( com.bargadss.SpringService.DAO )有两个主要操作
- getAllUser() – >查询数据库以从用户表中选择所有用户并返回列表{用户}
- getUserById(int user_ID) – >查询数据库以根据ID 选择特定用户
SpringServiceController ( com.bargadss.SpringService.Controller )如下:
package com.bargadss.SpringService.Controller; import java.util.List; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.bargadss.SpringService.DAO.UserService; import com.bargadss.SpringService.Domain.User; @RestController @RequestMapping("/service/user/") public class SpringServiceController { UserService userService=new UserService(); @RequestMapping(value = "/{id}", method = RequestMethod.GET,headers="Accept=application/json") public User getUser(@PathVariable int id) { User user=userService.getUserById(id); return user; } @RequestMapping(method = RequestMethod.GET,headers="Accept=application/json") public List getAllUsers() { List users=userService.getAllUsers(); return users; } }
ListUserController ( com.bargadss.SpringService.Controller )如下:
package com.bargadss.SpringService.Controller; import java.util.LinkedHashMap; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.client.RestTemplate; import org.springframework.web.servlet.ModelAndView; import com.bargadss.SpringService.Domain.User; @Controller public class ListUserController { @RequestMapping("/listUsers") public ModelAndView listUsers() { RestTemplate restTemplate = new RestTemplate(); String url="http://localhost:8080/SpringServiceWithRESTAndJSONExample/service/user/"; List users=restTemplate.getForObject(url, List.class); return new ModelAndView("listUsers", "users", users); } @RequestMapping("/dispUser/{userid}") public ModelAndView dispUser(@PathVariable("userid") int userid) { RestTemplate restTemplate = new RestTemplate(); String url="http://localhost:8080/SpringServiceWithRESTAndJSONExample/service/user/{userid}"; User user=restTemplate.getForObject(url, User.class,userid); return new ModelAndView("dispUser", "user", user); } }
CorsFilter ( com.bargadss.CORS )如下:
package com.bargadss.CORS; import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.filter.OncePerRequestFilter; public class CorsFilter extends OncePerRequestFilter{ @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) { // CORS "pre-flight" request response.addHeader("Access-Control-Allow-Origin", "*"); response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); response.addHeader("Access-Control-Allow-Headers", "Content-Type"); response.addHeader("Access-Control-Max-Age", "1800");//30 min } filterChain.doFilter(request, response); } }
web.xml如下:
SpringServiceWithRESTAndJSONExample rest org.springframework.web.servlet.DispatcherServlet 1 jsp org.apache.jasper.servlet.JspServlet rest /* jsp /WEB-INF/jsp/* cors com.bargadss.CORS.CorsFilter cors /*
AngularJS从另一个Web服务器调用Web服务返回错误,指的是跨源资源共享问题!
必须在Controller端执行哪些更改才能消除错误?
为避免这种情况,是否需要在AngularJS方面进行任何更改?
检查cors-filter
下载cors-filter-2.1.2.jar
和java-property-utils-1.9.1.jar
。
mvn依赖
com.thetransactioncompany cors-filter 2.1.2 com.thetransactioncompany java-property-utils 1.9.1
并在web.xml
CORS com.thetransactioncompany.cors.CORSFilter CORS /*
- Java生成RSA密钥对并转换为PEM – 但OpenSSL函数PEM_read_bio_RSA_PUBKEY返回null
- gloud的Java。 PERMISSION_DENIED:Google Cloud Pub / Sub API(实验性)
- 启用AngularJS到Jersey的CORS发布请求
- 对于预检,响应具有无效的HTTP状态代码401,用于oauth / token
- Angularjs Post没有向Spring JWT发送头文件
- 如何为单页AngularJS应用程序实现基本的Spring安全性(会话管理)
- 预检的响应具有无效的HTTP状态代码:401 angular
- Java Server – 使用POST发送Push到Google Firebase Cloud
- 在预检响应中,Access-Control-Allow-Methods不允许使用方法DELETE
- 如何使用angular js spring mvc上传多部分文件
- 如何组合现有的Google App引擎后端和AngularJS Web应用程序?