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

Array of objects as parameter in Rest service

我有一个接收对象数组的休息服务,我需要将 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 消息的每一小段都会调用转换器 class,以说明传入消息的一个简单示例:

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 一直读到最后一条评论。