springCloud feign exception of: RequestHeader parameter is null, the key for the addition of braces {}

 

  Long time did not write a blog today, taking advantage of the weekend to problems encountered in the work of the comb (in the troubleshooting process and found that their ability to troubleshoot problems or too weak, needs to be strengthened).

  Recently the company springCloud the project, called through feign remote other services, the code below, you can see, here RequestHeader which I pass a String tenantId, when the test is no problem, but after the project on-line and found that some users call this result is not the time you want to return to the interface, of course, when most users are normal. Then we print the log for the call link, found that as long RequestHeader inside tenantId is null when an error occurs, Client passed tenantId is null when received as a parameter server "{tenantId}", you not wrong, that the key parameters of the above plus a brace. This somewhat bizarre.

/**
     * Dynamic user query to mention the amount (only use the card surface)
     * @param tenantId
     * @Param paramJson
     * @return
     */
    @RequestMapping(value = "${wk.url.quota}/api/quota/findDynamicQuotaByCustomerId", method = RequestMethod.POST)
    String findDynamicQuotaByCustomerId(@RequestHeader String tenantId, @RequestBody JSONObject paramJson);

  Through the investigation, to find information, find the source of the problem is due to feign underlying see below in red print. At that time springCloud version we are using Finchley.SR2. But the new version of springCloud which has fixed the problem.

public  static String the expand (String Template, the Map <String,?> the Variables) {
     // If no valid variables, skip expansion. 
    IF (. checkNotNull (Template, "Template") length () <. 3 ) {
       return Template;
    }
    checkNotNull(variables, "variables for %s", template);
    boolean inVar = false;
    StringBuilder was = new String Builder ();
    Builder the StringBuilder = new new the StringBuilder ();
     for ( char C: template.toCharArray ()) {
       Switch (C) {
         Case '{' :
           IF (INVAR) {
             // '{{' is the escape character, without parsing 
            builder .append ( "{" );
            inVar = false;
            break;
          }
          inVar = true;
          break;
        case '}':
          if (!inVar) {
            builder.append('}');
            break;
          }
          inVar = false;
          Key String = var.toString ();
           // Variables here is the header map, since the name of the header null value, so that there is only one value-Key 
          Object value = variables.get (var.toString ());
           IF (value! = null ) {
            builder.append(value);
          } The else {
           // "culprit" Here, the default initial value again when returned 
           builder.append ( '{') the append (Key) .append (. '}' );
          }
          var = new StringBuilder();
          break;
        default:
          if (inVar) {
            var.append(c);
          } else {
            builder.append(c);
          }
      }
    }
    return builder.toString();
  }

  So the solution is simple: 1) Upgrade springCloud version; 2) out of the parameters can not be null.

 

Reference blog: https: //www.jianshu.com/p/550fb3b5f533

 

Guess you like

Origin www.cnblogs.com/DDgougou/p/11964406.html