Reputation: 517
I am writing a client for my REST service using Spring. When I try to consume the service I get 400 response error. It's Bad Request so there must be something wrong with my headers or parameters but I can't figure out what in particular.
My REST controller method:
@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();
}
Client fetch method:
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());
}
Console when calling 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)
Service method response is String as you can see. This is for my college classes so I need to use @RequestHeader and @PathVariable. I think something might be wrong with my headers/parameters or with path variable count
which in fetchMenu()
method is represented as limit
.
I have no idea what might be wrong with my code. Please developers, help ;)
Upvotes: 1
Views: 3380
Reputation: 149
Please make the following change in the fetchMenu() method.
ResponseEntity<String> response = template.exchange(
"http://localhost:8080/menu/" + limit +"?includeVegan={includeVegan}&includeMeat={includeMeat}&includeGlutenFree={includeGlutenFree}",
HttpMethod.GET,
entity,
String.class,
parameters
);
Upvotes: 3