2017-12-04 37 views
0

私はSpringを使用してRESTサービス用のクライアントを作成しています。私がサービスを消費しようとすると、400の応答エラーが発生します。それは私のヘッダーやパラメータに間違った何かがある必要がありますので、私は特に何を把握することはできません要求が悪いです。RestTemplateカスタムヘッダーとパラメータを使用してGETリクエストを400(null)としました

マイRESTコントローラ方法:

@RequestMapping(value = "/menu/{count}", method = RequestMethod.GET) 
    public String showMenu(@RequestHeader(required = false) String locale, @RequestParam boolean includeVegan, 
          @RequestParam boolean includeMeat, @RequestParam boolean includeGlutenFree, 
          @PathVariable(value = "count") int count) { 

     List<Meal> filteredMeals = new ArrayList<>(); 

     if (count == 0) count = menuService.getMeals().size(); 

     // Tworzenie przefiltrowanego menu. 
     for (Meal meal : menuService.getMeals()) { 
      if (count <= 0) break; 
      if (meal.isVegan() && includeVegan) filteredMeals.add(meal); 
      if (!meal.isVegan() && includeMeat) filteredMeals.add(meal); 
      if (meal.isGlutenFree() && includeGlutenFree) filteredMeals.add(meal); 
      count--; 
     } 

     // Tworzenie nazw pozycji posiłków w wybranym języku. 
     List<String> menu = new ArrayList<>(); 
     for (Meal meal : filteredMeals) { 
      if (locale == null) { 
       // BOTH LANGUAGES 
       menu.add(meal.toString(true)); 
       menu.add(meal.toString(false)); 
      } else if (locale.equals("pl-PL")) { 
       // POLISH LANG 
       menu.add(meal.toString(true)); 
      } else if (locale.equals("en-US")) { 
       // ENGLISH LANG 
       menu.add(meal.toString(false)); 
      } 
     } 

     return menu.toString(); 
    } 

クライアントがメソッドフェッチ:あなたが見ることができるようfetchMenu()

DEBUG: locale: pl-PL, vegan false, meat: true, glutenFree: false, limit: 0 
16:57:37.082 [AWT-EventQueue-0] DEBUG org.springframework.web.client.RestTemplate - Created GET request for "http://localhost:8080/menu/0" 
16:57:37.082 [AWT-EventQueue-0] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/*+json, */*] 
16:57:37.097 [AWT-EventQueue-0] DEBUG org.springframework.web.client.RestTemplate - GET request for "http://localhost:8080/menu/0" resulted in 400 (null); invoking error handler 
Exception in thread "AWT-EventQueue-0" org.springframework.web.client.HttpClientErrorException: 400 null 
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:78) 
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700) 
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653) 
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:621) 
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:540) 
    at ait.lab3.SwingClient.fetchMenu(SwingClient.java:101) 
    at ait.lab3.SwingClient.access$000(SwingClient.java:16) 
    at ait.lab3.SwingClient$5.actionPerformed(SwingClient.java:65) 
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) 
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 
    at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:308) 
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) 
    at java.awt.Component.processMouseEvent(Component.java:6533) 
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) 
    at java.awt.Component.processEvent(Component.java:6298) 
    at java.awt.Container.processEvent(Container.java:2236) 
    at java.awt.Component.dispatchEventImpl(Component.java:4889) 
    at java.awt.Container.dispatchEventImpl(Container.java:2294) 
    at java.awt.Component.dispatchEvent(Component.java:4711) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) 
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525) 
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) 
    at java.awt.Container.dispatchEventImpl(Container.java:2280) 
    at java.awt.Window.dispatchEventImpl(Window.java:2746) 
    at java.awt.Component.dispatchEvent(Component.java:4711) 
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) 
    at java.awt.EventQueue.access$500(EventQueue.java:97) 
    at java.awt.EventQueue$3.run(EventQueue.java:709) 
    at java.awt.EventQueue$3.run(EventQueue.java:703) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90) 
    at java.awt.EventQueue$4.run(EventQueue.java:731) 
    at java.awt.EventQueue$4.run(EventQueue.java:729) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) 

サービスメソッドの応答を呼び出すとき

private void fetchMenu() { 

     String locale = languageComboBox.getSelectedItem().toString(); 
     boolean includeVegan = includeVeganCheckBox.isSelected(); 
     boolean includeMeat = includeMeatCheckBox.isSelected(); 
     boolean includeGlutenFree = includeGlutenFreeCheckBox.isSelected(); 
     int limit = (int) limitSpinner.getValue(); 

     System.out.println("DEBUG: locale: " + locale + ", vegan " + includeVegan + ", meat: " + includeMeat + 
       ", glutenFree: " + includeGlutenFree + ", limit: " + limit); 

     HttpHeaders headers = new HttpHeaders(); 
     headers.add("locale", locale); 

     HttpEntity entity = new HttpEntity(headers); 

     Map<String, Object> parameters = new HashMap<>(); 
     parameters.put("includeVegan", includeVegan); 
     parameters.put("includeMeat", includeMeat); 
     parameters.put("includeGlutenFree", includeGlutenFree); 

     RestTemplate template = new RestTemplate(); 
     ResponseEntity<String> response = template.exchange(
       "http://localhost:8080/menu/" + limit, 
       HttpMethod.GET, 
       entity, 
       String.class, 
       parameters 
     ); 

     menu.setText(response.getBody()); 
    } 

コンソールを文字列です。これは私の大学のクラスのため、@RequestHeaderと@PathVariableを使う必要があります。私は、ヘッダ/パラメータ、またはとして表されるfetchMenu()の方法では、パス変数countで何かが間違っていると思う。

私のコードに何が間違っているかわかりません。助けてください;)

答えて

1

fetchMenu()メソッドで次のように変更してください。

ResponseEntity応答= template.exchange( "http://localhost:8080/menu/" +リミット+ "?includeVegan = {includeVegan} & includeMeat = {includeMeat} & includeGlutenFree = {includeGlutenFree}"、 HttpMethod.GET、 エンティティ、 パラメータ: )

+0

したがって、exchange()メソッドのuriParametersパラメータは何をしますか?私のURIにパラメータを追加するべきではありませんか? – Mesayah

+1

新しいパラメータを追加することはなく、uriParametersマップのキーに対応する値でトークンを置き換えます。 –

関連する問題