In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the springboot cross-domain problem brought BUG how to solve the relevant knowledge, detailed and easy to understand, simple and fast operation, with a certain reference value, I believe that everyone reading this springboot cross-domain problem brought BUG how to solve the article will have some harvest, let's take a look at it.
demand
The front-end uses a rich text plug-in Ueditor, which needs to get config configuration from the back-end when initializing.
Pit entry experience
Let's look at the initial code:
@RequestMapping(value = "/getConfig") public Object getConfig(HttpServletRequest request){ return readConfig(); } /** * read the configuration file * @return */ private UedConfig readConfig() { String path = this.getClass().getResource("/").getPath(); FileInputStream fileInputStream = new FileInputStream(path + "config/ued_config.json"); //read it out, turn it into an object and return ... }
The code looks something like this, and then when it starts up, the front end tells me that I didn't get the information. Nani, my postman self-test is fine, there are data returned. Then, Ah Fan ran to ask the front end, did he report anything wrong? The front end says it's cross-domain. At that time, A Fan was not happy, immediately ctrl+c plus ctrl+v sent the configuration of the backend processing cross-domain requests to the front end. Then the front end tells me it's jsonp requesting cross-domain. Ah Fan was dumbfounded. Jsonp cross-domain? Never heard of it. Immediately find the mother, indeed, a search is all the way to deal with. Because it is a springboot project, I found the simplest one. Look at the code:
@ControllerAdvicepublic class JsonpAdvice extends AbstractJsonpResponseBodyAdvice { public JsonpAdvice(){ super("callback"); }}
See, it's easy. It would be perfect if there was no red line under Abstract J son p Response Body Advice. No classes introduced? It's hard to pour a powder. alt + enter, uh-huh, what's the situation, there's no such category? That's impossible. Ask your mother immediately, it turns out that this class is only available under Springboot 2.0. What should we do? 2.0 There seems to be no way to handle jsonp cross-domain. Ah Fan searched Baidu again, and sure enough, heaven pays off. Ah Fan saw that jackson had a class JSONPObject that could be handled, and then Ah Fan changed the code:
@RequestMapping(value = "/getConfig")public Object getConfig(String callback,HttpServletRequest request){ return new JSONPObject(callback,readConfig());}
Then restart, self-test no problem. Let the front end try, you can get it normally. OK, perfect.
After the project is completed, the front and back ends are also docked, and sent to the test environment for testers to test. Duang~ A bug thrown on a pink head, failed to get configuration. Impossible ah, it must be the front end of the problem, run to find the front end so that the front end to see. The front-end looked at it and gave me a sentence: the test environment does not cross domains. In an instant, 10,000 heads of grass floated over his head. A fan labored for hours to deal with cross-domain problems, and you told me that the test environment does not cross domains.
Sigh, there was nothing he could do. He could only bear it with tears. Then Fan changed the code again:
@RequestMapping(value = "/getConfig")public Object getConfig(String callback,HttpServletRequest request){ return StringUtils.isEmpty(callback) ? readConfig() : new JSONPObject(callback,readConfig());}
Or do a compatibility, because the front end also needs to connect me to debug locally. However, there shouldn't be any problems this time. Ah Fan was still a little proud. Submit the code, send the test, get it done.
Duang~ The same bug was thrown at Ah Fan's face again. My heart is cold. What's going on? The logs are correct. After a long time, I don't know what the reason is. Ah Fen can only write log, because it is not easy to debug in the test environment, and there is no error. Then let the operation and maintenance cooperate, and then find that the path obtained by this.getClass().getResource("/").getPath() is incorrect. Ah Fan is confused again. Isn't this getting the project root path? What's going on.
And then a pink and cheeky looking for the mother, sure enough to find the reason, because springboot integrated tomcat, the project is directly run as a jar package, can not be obtained through this.getClass().getResource("/").getPath() this way to get the root path of the project, can only be obtained through the flow, and then a pink and changed the code:
/** * read the configuration file * @return */private UedConfig readConfig() { InputStream resourceAsStream = this.getClass().getResourceAsStream("/"+"config/ued_config.json"); //read it out, turn it into an object and return ...} About "how to solve the BUG brought by springboot cross-domain problem" The content of this article is introduced here, thank you for reading! I believe everyone has a certain understanding of the knowledge of "how to solve BUG caused by springboot cross-domain problems." If you still want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.