本文介绍了使用Spring MVC在REST HTTP GET请求中传递JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此 REST模型以及我认为关于REST的共识:每个REST 检索都应作为HTTP GET请求执行.现在的问题是如何使用Spring MVC将复杂的JSON对象作为GET请求中的参数.我发现还有另一个共识,那就是使用POST进行检索!"只是因为大公司会这样做!",但已要求我尝试遵守"REST 2级规则".

According to this REST model and to what I think is a consensus on REST: every REST retrieval should be performed as a HTTP GET request.The problem now is how to treat complex JSON objects as parameters in GET requests with Spring MVC.There is also another consensus I found saying "use POST for retrievals!" just because "the big companies do this!", but I have been asked to try to stick to the "REST level 2 rules".

第一个问题:我是否想做一些有意义的事情?

First question: am I trying to do something that make sense?

我想在带有Spring MVC的Java中通过GET请求发送数组/列表/组JSON对象.我无法弄清楚我的尝试出了什么问题,我尝试添加/删除双引号,并使用URL参数,但是我无法实现此目标.

I want to send via GET requests arrays / lists / sets of JSON objects, in Java with Spring MVC.I can not figure out what's wrong with my attempts, I have tried to add/remove double quotes, played around with the URL parameters, but I can not achieve this goal.

以下代码有什么问题?该代码段来自MVC控制器.

What's wrong with the following code? The code snippet is coming from a MVC controller.

@RequestMapping(
        value = "/parseJsonDataStructures",
        params = {
                "language",
                "jsonBeanObject"

        }, method = RequestMethod.GET, headers = HttpHeaders.ACCEPT + "=" + MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public HttpEntity<ParsedRequestOutputObject> parseJsonDataStructures(

        @RequestParam String language,
        @RequestParam CustomJsonBeanObject[] customJsonBeanObjects){

    try {
        ParsedRequestOutputObject responseFullData = customJsonBeanObjectService.parseJsonDataStructures(customJsonBeanObjects, language);

        return new ResponseEntity<>(responseFullData, HttpStatus.OK);
    } catch (Exception e) {
        // TODO
    }
}

我尝试了多种方法来构建URL请求(总是收到HTTP代码400错误请求),这是一个示例:

I've tried multiple ways to build the URL request (always getting HTTP code 400 Bad Request), this is an example:

http://[...]/parseJsonDataStructures?language=en&jsonBeanObject={"doubleObject":10, "enumObject":"enumContent", "stringObject":"stringContent"}

JSON对象变量ar类型:

The JSON object variables ar of type:

  • 双精度(不是原始精度)
  • 枚举
  • 字符串

我假设我可以一个接一个地传递多个jsonBeanObject参数.

I am assuming I can pass multiple jsonBeanObject parameters one after the other.

jsonBeanObject Java Bean是:

The jsonBeanObject Java bean is:

public class CustomJsonBeanObject {

    private Double doubleObject;
    private CustomEnum enumObject;
    private String stringObject;

    /**
     * Factory method with validated input.
     *
     * @param doubleObject
     * @param enumObject
     * @param stringObject
     * @return
     */
    public static CustomJsonBeanObject getNewValidatedInstance(Double doubleObject, CustomEnum enumObject, String stringObject) {
        return new CustomJsonBeanObject
                (
                        doubleObject ,
                        enumObject   ,
                        stringObject
                );
    }

    private CustomJsonBeanObject(Double doubleObject, CustomEnum enumObject, String stringObject) {
        this.doubleObject = doubleObject;
        this.enumObject = enumObject;
        this.stringObject = stringObject;
    }


    public CustomJsonBeanObject() {}

    // getter setter stuff

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}

推荐答案

首先创建一个请求bean,其中应封装您期望作为请求一部分的参数.将其称为SomeRequest

First create a request bean which should encapsulate the parameters you expect as part of request. Will call it as SomeRequest

第二个在控制器中使用@RequestBody而不是@RequestParam

Second in the controller use @RequestBody instead of @RequestParam

@ResponseBody public HttpEntity<ParsedRequestOutputObject> parseJsonDataStructures(@RequestBody SomeRequest someRequest) { ... }

第三次使用RestTemplate作为客户端来调用您的REST服务.下面的示例代码-

Third use RestTemplate as client to invoke your REST services. Sample code below -

SomeRequest someRequest = new SomeRequest();
// set the properties on someRequest
Map<String, String> userService = new HashMap<String, String>();
RestTemplate rest = new RestTemplate();
String url = endPoint + "/parseJsonDataStructures";
ResponseEntity<SomeResponse> response = rest.getForEntity(url, someRequest,
      SomeResponse.class, userService);
SomeResponse resp = response.getBody();

这篇关于使用Spring MVC在REST HTTP GET请求中传递JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:49