我正在使用Spring为我的REST服务编写客户端。当我尝试使用该服务时,我得到400响应错误。这是错误的请求,所以我的标题或参数一定有问题,但我无法弄清楚具体是什么。
我的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();
}
客户端获取方法:
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());
}
调用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)
您可以看到服务方法响应是String。这是我的大学课程所以我需要使用@RequestHeader和@PathVariable。我认为我的标头/参数或路径变量count
可能有问题,fetchMenu()
方法将其表示为limit
。
我不知道我的代码可能出现了什么问题。请开发者,帮助;)
答案 0 :(得分:1)
请在fetchMenu()方法中进行以下更改。
ResponseEntity response = template.exchange( 的&#34; http://localhost:8080/menu/&#34; + limit +&#34;?includeVegan = {includeVegan}&amp; includeMeat = {includeMeat}&amp; includeGlutenFree = {includeGlutenFree}&#34;, HttpMethod.GET, 实体, String.class, 参数 );