1.读取配置文件
2.处理跨域问题(过滤器/@CrossOrigin注解)
一 读取配置文件
1.1@Value注解 默认读取application.properties,中文会乱码
1.2 @ConfigurationProperties @PropertySource
application.properties
server.port=8084 student.sno=007 student.sname=邦德 student.age=40
my.properties
student2.sno=007-2 student2.sname=邦德-2 student2.age=40-2
package com.ligy.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class Student { @Value("${student.sno}") private String sno; @Value("${student.sname}") private String sname; @Value("${student.age}") private String age; @RequestMapping("/test") @ResponseBody public String Test() { String result = "hello,world"; String config = "sno=" + sno + "|sname=" + sname + "|age=" + age; result += config; return result; } }
package com.ligy.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @ConfigurationProperties(prefix = "student2") @PropertySource(value = "classpath:my.properties", encoding = "UTF-8") @Component public class Student2 { @Value("${student.sno}") private String sno; @Value("${student.sname}") private String sname; @Value("${student.age}") private String age; public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } @RequestMapping("/test2") @ResponseBody public String Test2() { String result = "hello,world"; String config = "sno=" + sno + "|sname=" + sname + "|age=" + age; result += config; return result; } }
二 处理跨域问题(过滤器、@CrossOrigin注解)
2.1html调用接口,报跨域问题
2.2解决跨域问题
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("#div1").load("http://localhost:8084/test2"); }); }); </script> </head> <body> <div id="div1"><h2>使用 jQuery AJAX 修改文本内容</h2></div> <button>获取外部内容</button> </body> </html>
coreFilter
package com.ligy; import org.springframework.stereotype.Component; import javax.servlet.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Component public class coreFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) servletResponse; response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With"); filterChain.doFilter(servletRequest, servletResponse); } }