gpt4 book ai didi

java - 带有自定义 header 和参数的 RestTemplate GET 请求导致 400(空)

转载 作者:行者123 更新时间:2023-11-30 06:23:45 25 4
gpt4 key购买 nike

我正在使用 Spring 为 REST 服务编写客户端。当我尝试使用该服务时,收到 400 响应错误。这是错误的请求,所以我的 header 或参数一定有问题,但我不知 Prop 体是什么。

我的 REST Controller 方法:

@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)

如您所见,服务方法响应是字符串。这是我的大学类(class),所以我需要使用 @RequestHeader 和 @PathVariable。我认为我的 header /参数或路径变量 count 可能有问题,在 fetchMenu() 方法中表示为 limit

我不知道我的代码可能有什么问题。请开发人员帮忙;)

最佳答案

请在 fetchMenu() 方法中进行以下更改。

ResponseEntity<String> response = template.exchange(
"http://localhost:8080/menu/" + limit +"?includeVegan={includeVegan}&includeMeat={includeMeat}&includeGlutenFree={includeGlutenFree}",
HttpMethod.GET,
entity,
String.class,
parameters
);

关于java - 带有自定义 header 和参数的 RestTemplate GET 请求导致 400(空),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47637445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com