1.读取配置文件
2.处理跨域问题(过滤器/@CrossOrigin注解)
一 读取配置文件
1.1@Value注解 默认读取application.properties,中文会乱码
1.2 @ConfigurationProperties @PropertySource
application.properties
server.port=8084student.sno=007student.sname=邦德
student.age=40
my.properties
student2.sno=007-2student2.sname=邦德-2student2.age=40-2
packagecom.ligy.controller;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.ResponseBody;
@Controllerpublic classStudent {
@Value("${student.sno}")privateString sno;
@Value("${student.sname}")privateString sname;
@Value("${student.age}")privateString age;
@RequestMapping("/test")
@ResponseBodypublicString Test() {
String result= "hello,world";
String config= "sno=" + sno + "|sname=" + sname + "|age=" +age;
result+=config;returnresult;
}
}
packagecom.ligy.controller;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.PropertySource;importorg.springframework.stereotype.Component;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.ResponseBody;
@Controller
@ConfigurationProperties(prefix= "student2")
@PropertySource(value= "classpath:my.properties", encoding = "UTF-8")
@Componentpublic classStudent2 {
@Value("${student.sno}")privateString sno;
@Value("${student.sname}")privateString sname;
@Value("${student.age}")privateString age;publicString getSno() {returnsno;
}public voidsetSno(String sno) {this.sno =sno;
}publicString getSname() {returnsname;
}public voidsetSname(String sname) {this.sname =sname;
}publicString getAge() {returnage;
}public voidsetAge(String age) {this.age =age;
}
@RequestMapping("/test2")
@ResponseBodypublicString Test2() {
String result= "hello,world";
String config= "sno=" + sno + "|sname=" + sname + "|age=" +age;
result+=config;returnresult;
}
}
二 处理跨域问题(过滤器、@CrossOrigin注解)
2.1html调用接口,报跨域问题
2.2解决跨域问题
菜鸟教程(runoob.com)$("button").click(function(){
$("#div1").load("http://localhost:8084/test2");
});
});