在Java中使用带有CrossOrigin批注的Spring属性或在Spring-Config XML中使用Spring属性 [英] Use Spring Properties in Java with CrossOrigin Annotation or in Spring-Config XML

查看:130
本文介绍了在Java中使用带有CrossOrigin批注的Spring属性或在Spring-Config XML中使用Spring属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring中使用CrossOrigin注释. 现在,我想将属性作为值注入到注释中.我没有这个工作.

I use the CrossOrigin annotation in Spring. Now i want to inject a Property as value to the annotation. I dont get this to work.

现在我这样访问我的财产:

Now i access my property like this:

@Value("${settings.cors_origin}")
String cors_origin;

我想将此属性注入CrossOrigin批注:

I want to inject this property to the CrossOrigin annotation:

....
@CrossOrigin(origins = cors_origin)
@RequestMapping(value="/request", method= RequestMethod.GET, produces = "application/json")
public ResponseEntity<?> getMethod()
{
...

我已经尝试过这样的事情:

I tried already something like this:

@CrossOrigin(origins = "${settings.cors_origin}")

现在,我尝试在我的spring配置中全局设置CORS-Header:

Now i try to set the CORS-Header Globally in my spring config:

  <context:property-placeholder location="classpath*:*appsettings.properties"/>

 <mvc:cors>
 <mvc:mapping path="/api/**"
             allowed-origins="${test}"
             allowed-methods="GET, PUT, OPTIONS, POST"/>
</mvc:cors>

此设置也不起作用!允许的来源与属性文件中指定的来源不同.我认为它不会将变量转换为值吗?当我在spring config allow-origins中手动设置IP地址时,它正在工作.设置有问题...

This setting is also not working! The allowed origin is not the same as the one specified in the properties file. I think it will not translate the variable to the value? When i set the IP-Address in the spring config allowed-origins manually it is working. There is something wrong with the settings...

appsettings.properties:

test=http://192.168.1.200

S O L V E D

经过一段时间的故障排除,我现在为我解决了问题:-) 现在,我再次使用@CrossOrigin注释.我必须将 RequestMethod选项 添加到Spring RequestMapping:

I solved it for me now after a hart time of troubleshooting :-) Now i work again with the annotation @CrossOrigin. I had to add the RequestMethod Options to the Spring RequestMapping:

 @RestController
 @CrossOrigin(origins = {"${settings.cors_origin}"})
 @RequestMapping("/api/v1")
 public class MainController {


  @RequestMapping(value="/request", method = {RequestMethod.GET, RequestMethod.OPTIONS}, produces = "application/json")
  public ResponseEntity<?> getMethod()
  {

感谢您的帮助!

推荐答案

,您需要将字符串数组传递给@CrossOrigin批注的origins参数.这是用花括号完成的:

you need to pass an array of strings to the origins argument of the @CrossOrigin annotation. This is done with curly braces:

@CrossOrigin(origins = {"${settings.cors_origin}"})

这篇关于在Java中使用带有CrossOrigin批注的Spring属性或在Spring-Config XML中使用Spring属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆