2016-07-27 17 views
0
index.html 

    <!DOCTYPE html> 
    <html> 
    <head> 

    <title>Spring Security Example - ProgrammingFree</title> 
    </head> 
    <!-- <link href="<spring:url value="css/app.css" /> --> 
    <body class="security-app"> 
     <div class="details"> 
      <h2>Spring Application HBase Elasticsearch</h2> 

     </div> 
     <div class="lc-block"> 
      <h1>Welcome!</h1> 

      <div class="alert-normal"> 

       Click <a href="/es.html">here</a> to see a greeting. 

      <!-- Click<a href="/eshtml" onclick="this.form.submit()">here</a> --> 
      </div> 
     </div> 
    </body> 
    </html> 

    **es.html** 

    <!DOCTYPE html> 
    <html> 
    <head> 
    <!-- <link href="<c:url value="css/app.css" /> --> 
     <script type="text/javascript"> 

     function script(){ 
     var companyName = document.getElementById('companyName').value; 
     var contains = document.getElementById('contains').value; 
     var console = document.getElementById('console'); 
     var esControllerCall="/ess?companyName="+companyName+"&contains="+contains"; 
     var source = new EventSource(esControllerCall); 
     source.onmessage = function (event) { 

      var a = document.createElement('a'); 
      var data=event.data; 
      //alert(data); 
      var dataarray=data.split('tid-'); 
      //alert(dataarray); 
      a.appendChild(document.createTextNode(dataarray[0])); 
      var p = document.createElement('p'); 

      p.style.wordWrap = 'break-word'; 
      p.appendChild(a); 
      console.appendChild(p); 
      console.scrollTop = console.scrollHeight; 

     }; 
     } 
     </script> 
    <title>ES DATA</title> 
    </head> 
    <body class="security-app"> 
     <body class="security-app" onsubmit="script();> 
     <div class="details"> 
      <h2>Spring Application Authentication</h2> 
     </div> 

     <div class="lc-block"> 

      <form action="/ess" method="post"> 
     <label>Company Name</label> <input type="text" name="companyName" id="companyName"/></br> 
     <label>Contains Word</label> <input type="text" name="contains" id="contains" /></br> 

       <input type="submit" class="button red big" value="Search " /> 
       <!-- <input 
        type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" /> --> 
       <div id="console" style="width: 400px; height: 400px; overflow-y: scroll;"></div> 

      </form> 
     </div> 
     </body> 
    </html> 


<!-- end snippet --> 

**strong text** 

escontroller 

package com.microservice; 

@RestController 
public class ESController { 

    @RequestMapping(value = "/ess", method = RequestMethod.POST) 
    public List<String> dataFromEs(@RequestParam("companyName") String companyName, 
      @RequestParam("contains") String contains) 
      throws UnknownHostException { 
     Client client; 

     client = TransportClient 
       .builder() 
       .build() 
       .addTransportAddress(
         new InetSocketTransportAddress(InetAddress 
           .getByName("10.10.167.157"), 9300)); 

     SearchResponse response = client.prepareSearch(companyName) 
       .setQuery(QueryBuilders.matchQuery("text", contains)) 
       .setSize(200).execute().actionGet(); 

     System.out.println("response received is: "+response); 

     List<String> valuesList = new ArrayList<String>(); 
     for (SearchHit hit : response.getHits().getHits()) { 

      valuesList.add(hit.getSource().get("text").toString()); 
      valuesList.add(hit.getSource().get("id").toString()); 
      valuesList.add(hit.getSource().get("epochTime").toString()); 
     } 

     return valuesList; 

    } 
} 

esservice 

package com.microservice; 

@SpringBootApplication 
@EnableEurekaClient 

public class ESService { 
      @Autowired 
    ESController esController = new ESController(); 
     public static void main(String[] args) { 
     SpringApplication.run(ESService.class, args); 
    } 
} 

Die unten ist der Code für das Konsumieren microservice.ie Heureka-Client oder Entdeckung Client erhalten die microservices.and können wir keine post-Methode implementieren und nicht in der Lage es auf UI überprüfenWir wollen 10 Tweets in einer Liste anzuzeigen Micro auf Heureka registriert verwenden, aber ich bin immer 405 Fehler

package com.MasterService; 

@SpringBootApplication 
@EnableDiscoveryClient 
@RestController 
public class MasterService { 

    public static void main(String[] args) throws MalformedURLException { 

     SpringApplication.run(MasterService.class, args); 

    } 

    @Bean 
    RestTemplate restTemplate() { 
     return new RestTemplate(); 

    } 

    @Autowired 
    RestTemplate restTemplate; 

    @Autowired 
    private DiscoveryClient discoveryClient; 

    private List<String> listofValue; 

    private String indexName; 

    @RequestMapping(value = "/ess", method = RequestMethod.POST) 
    public List<String> dataFromEs(
      @RequestParam("companyName") String companyName, 
      @RequestParam("contains") String contains) 
      throws InterruptedException, ClientProtocolException, IOException { 

     ServiceInstance sinstance = this.discoveryClient.getInstances(
       "es-service").get(0); 

     URI storesUri = URI.create(String.format("http://%s:%s/dataFromEs/", 
       sinstance.getHost(), sinstance.getPort())); 

     System.out.println("## " + storesUri.toString() + " ##"); 

     indexName = companyName; 

     System.out.println("indexName----> " + indexName); 

     String name = "tweet_" + indexName + "_1467072000_1472256000"; 

     System.out.println("indexName----> " + name); 

     System.out.println("contains----------------->" + contains); 

     Map<String, Object> param = new HashMap<String, Object>(); 
     param.put("companyName", companyName); 
     param.put("contains", contains); 

     MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); 
     map.add("companyName", companyName + ""); 
     map.add("contains", contains + ""); 

     HttpClient client = HttpClientBuilder.create().build(); 
     HttpPost post = new HttpPost(storesUri.toString()); 

     List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); 
     urlParameters.add(new BasicNameValuePair("companyName", companyName 
       + "")); 
     urlParameters.add(new BasicNameValuePair("contains", contains + "")); 

     post.setEntity(new UrlEncodedFormEntity(urlParameters)); 

     HttpEntity<String> entity = new HttpEntity<String>(map); 

     String stringResult = restTemplate().postForObject(storesUri, map, 
       String.class); 

     System.out.println("-------- " + stringResult); 

     HttpResponse response = client.execute(post); 
     System.out.println("Response Code : " 
       + response.getStatusLine().getStatusCode()); 

     BufferedReader rd = new BufferedReader(new InputStreamReader(response 
       .getEntity().getContent())); 

     StringBuffer resultBuffer = new StringBuffer(); 
     String line = ""; 
     while ((line = rd.readLine()) != null) { 
      resultBuffer.append(line); 
     } 
     String result = resultBuffer.toString(); 

     listofValue.add(result); 
     // indexName = "tweet_" + companyName + "_1467072000_1472256000"; 


     return listofValue; 
    } 
} 

**the below is the error that i am trying to solve it.** Please do help us where we are going wrong . 

wir erhalten 405 Fehler. (405 Methode nicht erlaubt) Vielen Dank im Voraus Ausnahme.

**org.springframework.web.client.HttpClientErrorException: 405 Method Not Allowed** 
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:572) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:375) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
    at 

I am unable to resolve this error. 
Thanks in advance. 
+0

Sollte 'storysUri' nicht' "http: //% s:% s/esss /" 'anstelle von' "http: //% s:% s/dataFromEs /" 'sein? – Val

+0

Danke für die Antwort Seine Arbeit –

+0

Ich habe nicht durch js Teil des Codes gegangen, aber die Fehler auf Ihrem Stacktrace gegeben, rufen Sie entweder die Endpunkte mit HTTP-Methode GET anstelle von POST, da beide Ihre Ressourcen-Deklarationen POST erwarten, oder besser noch ändern Sie Ihre Methode zu GET, da Sie nur eine Liste zurückgeben. 'RequestMethod.GET' – geneqew

Antwort

0

Auf den ersten Blick, ich denke, das Problem Ihre storesUri ist, sollte es so aussehen:

             change this 
                  | 
                  v 
    URI storesUri = URI.create(String.format("http://%s:%s/ess/", 
      sinstance.getHost(), sinstance.getPort())); 

dh ersetzen dataFromEs (der Methodenname) von ess (der Endpunktname)