gpt4 book ai didi

java - 对象数组作为 Rest 服务中的参数

转载 作者:行者123 更新时间:2023-11-30 11:07:49 33 4
gpt4 key购买 nike

我有一个接收对象数组的休息服务,我需要将 json 信息转换回对象列表;我的堆栈构建在 Spring 4 之上

我得到了这个服务定义:

@RequestMapping(value = "/services/crud/dangers/createDanger", method = RequestMethod.GET)
public @ResponseBody GenericServiceReturn createDanger(
@RequestParam(value = "postionId", required = true) Long positionId,
@RequestParam(value = "dangerName", required = true) String dangerName,
@RequestParam(value = "type", required = true) Integer type,
@RequestParam(value = "description", required = true) String description,
@RequestParam(value = "words", required = true) List<RestWord> words)

可以看到,参数words是一个RestWord的List,定义如下:

public class RestWord {
private long id = -1;
private String name;
private long type = -1;

我还这样定义了转换器:

public class RestWordConverter implements Converter<String, RestWord>{
private static final Logger log = LoggerFactory
.getLogger(RestWordConverter.class);

@Override
public RestWord convert(String text) {
// TODO Auto-generated method stub
log.info(text);
return new RestWord();
}

}

到目前为止,您所看到的逻辑并不多,还像这样在 mvc 上下文中注册了转换器。

<mvc:annotation-driven conversion-service="conversionService" />

<beans:bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" >
<beans:property name="converters">
<beans:list>
<beans:bean class="co.com.lineascontrol.core.endpoints.converters.RestWordConverter"/>
</beans:list>
</beans:property>
</beans:bean>

问题是,为 json 消息的每一小段调用转换器类,以说明传入消息的一个简单示例:

String words = "[{\"id\":0,\"name\":instalar,\"type\":-1},{\"id\":0,\"name\":ventilacion,\"type\":-1},{\"id\":0,\"name\":tunel,\"type\":-1}]";

我在服务器输出中得到这个:

c.c.l.c.e.c.RestWordConverter - [{"id":0
c.c.l.c.e.c.RestWordConverter - "name":instalar
c.c.l.c.e.c.RestWordConverter - "type":-1}
c.c.l.c.e.c.RestWordConverter - {"id":0
c.c.l.c.e.c.RestWordConverter - "name":ventilacion
c.c.l.c.e.c.RestWordConverter - "type":-1}
c.c.l.c.e.c.RestWordConverter - {"id":0
c.c.l.c.e.c.RestWordConverter - "name":tunel
c.c.l.c.e.c.RestWordConverter - "type":-1}]

这意味着对消息的每一部分都调用一次转换器函数,这样做是不可能将传入的字符串转换为特定对象的。

我还有这样定义的标准 Json 转换器:

<beans:bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter" />
</beans:list>
</beans:property>
</beans:bean>

<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>

我认为转换器应该定义为 json 转换器,而不是 mvc 的转换服务,但我还没有设法找到关于此的示例或文档。

解决方案

您好,如果其他人遇到这个问题,我设法让一切正常运行。

首先,如果您是 Rest 新手,我发现这篇简单的文章解释了基本但非常重要的内容:

http://www.drdobbs.com/web-development/restful-web-services-a-tutorial/240169069?pgno=1

在那之后,我明白了我的大部分问题,所以最后这是我的服务界面:

@RequestMapping(value = "/services/crud/dangers/createDanger", method = RequestMethod.POST)
public @ResponseBody GenericServiceReturn createDanger(
@RequestBody List<RestWord> words,
@RequestParam(value = "postionId", required = true) Long positionId,
@RequestParam(value = "dangerName", required = true) String dangerName,
@RequestParam(value = "type", required = true) String type,
@RequestParam(value = "description", required = true) String description) {

这里是调用它的正确方法!

List<RestWord> restWordList = new ArrayList<RestWord>();

//put some values in the list! or whatever object you are using

url = "http://localhost:8080/LineasControllBussinesLayer/rest/services/crud/dangers/createDanger";

//add the uri params
Map<String, Object> requestParams = new HashMap<String, Object>();
requestParams.put("postionId", 1l);
requestParams.put("dangerName", dangerName);
requestParams.put("type", DANGER_TYPE.ACTIVITIE);
requestParams.put("description", "actividad repeligrosa");

// Create the request body as a MultiValueMap

System.out.println(restWordList);
//see how the HttpEntity is created with the first parameter as the object, and the second are the header, in my case I use the headers to aunteticate
HttpEntity<List<RestWord>> entity2 = new HttpEntity<List<RestWord>>(restWordList, headers);


//then just call the service!!!
System.out.println(restTemplate.postForObject(url, entity2, String.class, requestParams));

请记住,这只是测试代码,实际的请求应该被包装到一个请求对象中,所有的东西都应该放在请求体中

最佳答案

我建议使用 @Resquestbody 将 json 映射到一个对象。请注意将它与 @RequestParam 结合使用,因为它可能工作或不给定版本,并且它必须按特定顺序(在这种情况下尽量避免 @ResquestParam ).

看看:Spring MVC - Why not able to use @RequestBody and @RequestParam together一直读到最后一条评论。

关于java - 对象数组作为 Rest 服务中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28749785/

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