I am new to java and am still trying to wrap around my mind around many of its concepts.
Right now in my application that pulls in data from an external api. I am trying to hardcode a path, for now, to make sure that I am getting the response I am expecting (this is a temporary interaction as eventually, I want the app to be stateless. If I pass a hardcoded value for @PathVariable in my controller with the variable defined above the code doesn't read the value.
Where should I be placing the hard-coded value and am I defining it the correct way?
Code:
String identificationCode ="abcd";
@RequestMapping(value ="/download/{identificationCode}", method = RequestMethod.GET)
String downloadDocument(@PathVariable(value="identificationCode") String identificationCode) {
.
.
.
}
value
is a alias for name. That means that @PathVariable(value="identificationCode")
specifies name of variable for this parameter, but not value. See
Here "/download/{identificationCode}"
identificationCode
is not interpolated by the value of the String declared there :
String identificationCode ="abcd";
It will just produce the String : "/download/{identificationCode}"
.
You could write it :
@RequestMapping(value ="/download/"+identificationCode, method = RequestMethod.GET)
but it will not work either as identificationCode
is not a constant expression.
So what you want is just :
@RequestMapping(value ="/download/abc", method = RequestMethod.GET)
Use this way if you don't need to reference the String somewhere else.
Otherwise as alternative declare identificationCode
as a constant expression (and you can also do this static
by the way) :
final static String identificationCode ="abcd";
And you could so use it :
@RequestMapping(value ="/download/"+identificationCode, method = RequestMethod.GET)
将{identificationCode}替换为@RequestMapping(value =“ / download / {identificationCode}”“中的硬编码值。稍后,当您需要路径的动态性质时,可以按照当前编码的方式进行操作。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.