方式一:
在springMVC4.2之前我们都是通过定义注册过滤器的方式来解决跨域与问题的.
过滤器:
public class CORSFilter implements Filter{
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) resp;
response.addHeader("Access-Control-Allow-Origin", "*");
chain.doFilter(req, resp);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
web.xml配置:略
方式二:
在Spring MVC4.2之后推出了@CrossOrigin注解来解决跨域问题。
@CrossOrigin
@RestController
public class VoiceAlarmController {
@Value("${ah.voicePath}")
String voicePath;
@RequestMapping(value="/strToVoice",method= RequestMethod.GET)
public String voiceAlarm(@RequestParam String words) throws Exception{
WordsConverter wc = new WordsConverter();
String fileId = UUID.randomUUID().toString() + ".mp3";
//调用工具类,把生成的语音文件存到本地
try {
wc.voice(voicePath + fileId, words);
} catch (Exception e) {
return "";
}
return fileId;
}