简体   繁体   English

如何检索从ajax调用传递给Java API的JSON

[英]How to retrieve JSON passed from ajax call to Java API

I am making a call to JAVA API as follows: 我正在按如下方式调用JAVA API:

var userDetails = {
            userId: userId,
            first : "1 one",
            second : "2 two"
        }
    $.ajax({
        type : 'POST',
        url : "http://" + config.domain + config.root + "/myExp/allExperiment",
        dataType : "json",
        data : userDetails, 
        success : function(data) {})
   });

And trying to get the passed object as follows: 并尝试获取传递的对象,如下所示:

@RequestMapping(value = "/allExperiment", method = RequestMethod.POST)
    public JsonMapModel getAllDatasets(@RequestBody String userDetails) {
         System.out.println("Data is " + userDetails);
}

I am getting following at the API Data is second=2+two&userId=16&first=1+one 我在API处获得关注数据是 second = 2 + two&userId = 16&first = 1 + one

Any idea how can I convert the above response to the JSONObject or any other collection so that I can refer the passed JSON appropriately. 知道如何将以上响应转换为JSONObject或任何其他集合,以便可以适当地引用传递的JSON。

You use jackson library. 您使用杰克逊图书馆。 Jackson Convert Java Object to / from JSON 杰克逊将Java对象与JSON转换

(pom.xml) (pom.xml)

<!-- Jackson -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.4.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.3</version>
</dependency>

(js) (js)

    var userDetails = {
            userId: userId,
            first : "1 one",
            second : "2 two"
        }

    userDetails = JSON.stringify(userDetails);       

    $.ajax({
        type : 'POST',
        url : "http://" + config.domain + config.root + "/myExp/allExperiment",
        contentType : 'application/json',
        data : userDetails,

        success : function(data) {

    },
    error : function(request, status, error) {

    }
});

(Model) (模型)

public class TestModel {

    private String userId;
    private String first;
    private String second;

    //getter, setter 
}

(Controller) (控制器)

@RequestMapping(value = "/allExperiment", method = RequestMethod.POST)
public @ResponseBody String getAllDatasets(@RequestBody TestModel userDetails) {


    return null; // break point, check model.
}

It should work if you replace String userDetails with a real object for example, like UserDetails userDetails). 如果将String userDetails替换为实际对象(例如UserDetails userDetails),则该方法应该起作用。

Or try to use a MultiValueMap instead of String. 或尝试使用MultiValueMap代替String。 From the link below it seems that if this is the parameter than Spring automatically uses a FormHttpMessageConverter. 从下面的链接看来,如果这是参数,Spring会自动使用FormHttpMessageConverter。 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestbody http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestbody

You can use Jersey API for conversion of JSON to Value Object at server side. 您可以使用Jersey API在服务器端将JSON转换为Value Object。 Jersey Jar does automatic conversion of JSON to VO and VO to JSON. Jersey Jar会自动将JSON转换为VO和将VO转换为JSON。 Below is the snap shot how you can receive VO at server side: 以下是如何在服务器端接收VO的快照:

@POST
@Path("/allExperiment")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public JsonMapModel  getAllDatasets(UserDetails userDetails) {
     System.out.println("Data is " + userDetails);
}

Thanks for the response, as per the suggestion by @Alexandru Marina, following worked for me: 感谢@Alexandru Marina的建议,以下回复对我有用:

@RequestMapping(value = "/allExperiment", method = RequestMethod.POST)
    public JsonMapModel getAllDatasets(@RequestBody MultiValueMap<String, String> userDetails) {
System.out.println("User id is "  userDetails.getFirst("userId")); }

Thanks Once Again 再次感谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM