In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "java background call interface and how to solve cross-domain problems". In daily operation, I believe many people have doubts about how to solve cross-domain problems in java background calling interfaces and dealing with cross-domain problems. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "java background calling interfaces and dealing with cross-domain problems". Next, please follow the editor to study!
Java calls interface and handles cross-domain
When working on a system, sometimes the js code of system A needs to call the interface of system B, which leads to a cross-domain phenomenon, which can be handled through background calls.
Problem, this is a bit of an "agent" meaning.
Record a general method public String httpPost (String urlStr,Map params) {URL connect; StringBuffer data = new StringBuffer (); try {connect = new URL (urlStr); HttpURLConnection connection = (HttpURLConnection) connect.openConnection (); connection.setRequestMethod ("POST"); connection.setDoOutput (true); connection.setDoInput (true); connection.setUseCaches (false) / / post cannot use cache connection.setInstanceFollowRedirects (true); connection.setRequestProperty ("accept", "* / *"); connection.setRequestProperty ("connection", "Keep-Alive"); connection.setRequestProperty ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1 user-agent)"); OutputStreamWriter paramout = new OutputStreamWriter (connection.getOutputStream (), "UTF-8") String paramsStr = ""; / / the parameter for (String param: params.keySet ()) {paramsStr + = "&" + param + "=" + params.get (param);} if (! paramsStr.isEmpty ()) {paramsStr = paramsStr.substring (1);} paramout.write (paramsStr); paramout.flush () BufferedReader reader = new BufferedReader (new InputStreamReader (connection.getInputStream (), "UTF-8")); String line; while ((line = reader.readLine ())! = null) {data.append (line);} paramout.close (); reader.close () } catch (Exception e) {/ / TODO Auto-generated catch block e.printStackTrace ();} return data.toString ();} Cross-domain problems caused by calling external interfaces
Background: on our system, an external suggestion system is referenced. After the user is given a comment or reply, my message shows the number of unread messages.
The effect of implementation: when the number of unread messages in the suggestion system is greater than 0, there will be red dots indicating unread messages in the position where our system introduces the recommendation system.
In the background of the recommendation system, we write an countBlog interface to get the number of unread messages (json format)
In the foreground of our system, the interface is introduced to control the red dot display by the number of unread messages returned.
Bug that reports cross-domain problems after running:
Solve the problem
Method 1: note @ CrossOrigin
Method 2: addCorsMappings configuration
@ Override public void addCorsMappings (CorsRegistry registry) {registry.addMapping ("/ * *") .allowedOrigins ("*") .allowedMethods ("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS") .allowCredentials (true) .maxAge (3600) .allowedHeaders ("*");}
Disadvantages: cross-domain related configurations will fail when you use this method to configure and then use a custom interceptor.
The reason is the order of the requests. When the request arrives, it will first enter the interceptor instead of the Mapping mapping, so there is no cross-domain information configured in the header information returned. The browser will report a cross-domain exception.
Method 3: use CorsFilter filter
Private CorsConfiguration corsConfig () {CorsConfiguration corsConfiguration = new CorsConfiguration (); * three commonly used configurations for requests. * indicates that all is allowed. At that time, you can also customize attributes (such as what can only be carried by header, only in post mode, etc.) * / corsConfiguration.addAllowedOrigin ("*"); corsConfiguration.addAllowedHeader ("*"); corsConfiguration.addAllowedMethod ("*"); corsConfiguration.setAllowCredentials (true); corsConfiguration.setMaxAge (3600L); return corsConfiguration } @ Beanpublic CorsFilter corsFilter () {UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource (); source.registerCorsConfiguration ("/ * *", corsConfig ()); return new CorsFilter (source);} at this point, the study on "java background calls the interface and how to deal with cross-domain problems" is over. I hope to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.