gpt4 book ai didi

java - Spring MVC Rest Web服务: using HashMap in request bean

转载 作者:太空宇宙 更新时间:2023-11-04 13:53:51 24 4
gpt4 key购买 nike

我有以下 Spring MVC Rest Controller :

@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/zoek", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
JsonExcelLijstList zoek(ZoekExcelLijstParameters parameters) {
// arbitraty code
}

ZoekExcelLijstParameters 对象如下所示:

public class ZoekExcelLijstParameters extends NgTableParams {
private String typeSleutel;
private String status;
private Date datumVan;
private Date datumTot;
// getters and setters
}

NgTableParams 看起来像这样

public class NgTableParams {
private int page = 1;
private int count = 20 ;
private HashMap<String, String> sorting; // HashMap because Jackson doesn't like interfaces (?)
// getters and setters
}

我已经配置了 Jackson Message Converter:

<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>

我使用 Angular.js 发送的数据如下所示(由 Chrome 开发工具表示,因此不是 JSON)

count:10
dateTot:
dateVan:
page:1
sorting:{"uploadDate":"asc", "uploader": "asc"}
status:
type:

当我这样做时,我收到以下错误:

BindException:org.springframework.validation.BeanPropertyBindingResult: 1 errors↵Field error in object 'zoekExcelLijstParameters' on field 'sorting': rejected value [{"uploadDate":"asc"}]; codes [typeMismatch.zoekExcelLijstParameters.sorting,typeMismatch.sorting,typeMismatch.java.util.HashMap,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [zoekExcelLijstParameters.sorting,sorting]; arguments []; default message [sorting]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.HashMap' for property 'sorting'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.HashMap] for property 'sorting': no matching editors or conversion strategy found]

我不知道我做错了什么。我还尝试使用带有键值对象的列表而不是 HashMap。

我见过很多解决方案,他们建议使用 POST 数据和请求正文,或者只是简单的 HashMap,但在这里我需要(希望)它与 GET 一起使用(因为它是“获取”操作而不是“创建”操作)。

最佳答案

抱歉,我没有时间重写您的代码,但这是我用于不同项目的解决方案:只需将它们放入 map 对象中即可

@RestController
public class NewServiceController
{
@Autowired
EsbcoreServiceRepository serviceRepo;
@Autowired
EsbcoreRuleRepository ruleRepo;
@Autowired
EsbcoreRuleConditionRepository ruleConditionRepo;
@Autowired
EsbcoreRuleDestinationRepository ruleDestinationRepo;
@Autowired
EsbcoreServiceDestinationRepository serviceDestinationRepo;

@GetMapping(value="/api/allservices")
public Map<String, Object> getAllServices()
{
Map<String, Object> servicesMap=new HashMap<String, Object>();
servicesMap.put("services", serviceRepo.findAll());
servicesMap.put("rules", ruleRepo.findAll());
servicesMap.put("ruleConditions", ruleConditionRepo.findAll());
servicesMap.put("ruleDestinations", ruleDestinationRepo.findAll());
servicesMap.put("serviceDestinations", serviceDestinationRepo.findAll());

return servicesMap;

}

关于java - Spring MVC Rest Web服务: using HashMap in request bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30030949/

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