简体   繁体   English

如何从AJAX请求中读取RestWebservice中的JSON数据

[英]How to read JSON data in RestWebservice from AJAX request

I am trying to send a JSON string from a HTML page using AJAX call to a RESTWebservice. 我正在尝试使用AJAX调用从RESTWebservice从HTML页面发送JSON字符串。 The methods in the server gets invoked however I am not able to retrieve the JSON data that I have set in the browser through AJAX call. 服务器中的方法被调用,但是我无法通过AJAX调用检索在浏览器中设置的JSON数据。 I am usng jersy for the REST services. 我很讨厌REST服务。

Here is my HTML code. 这是我的HTML代码。

 <body> <script type="text/javascript"> var userConfig = {}; userConfig.user = "arin_12"; userConfig.fullName = "Arindam"; var data = JSON.stringify(userConfig); alert(data); var req = new XMLHttpRequest(); req.open('POST', 'http://localhost:8080/LiveHive2/rest/hello', true); req.setRequestHeader('Content-Type','application/json;charset=UTF-8'); req.onreadystatechange = function () { if(req.readyState === 4 && req.status === 200) { if(req.responseText) { alert('The saving of data is ' + req.responseText); } } } req.send(data); </script> index page </body> 

Here is my JavaCode in RestWebservice. 这是我在RestWebservice中的JavaCode。

  @POST
  @Produces(MediaType.APPLICATION_JSON)
  @Consumes(MediaType.APPLICATION_JSON)
  public String sayJSONHello2(UserConfig uc) {

    System.out.println("req" + uc);  
    return "{\"Name\":\"Arindam\"}";
  }

Web.xml looks like this. Web.xml看起来像这样。

<servlet>
    <servlet-name>JerseyRESTService</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.vogella.jersey.first</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

So the body of the POST request will be 因此, POST请求的主体将是

{
    "user": "arin_12",
    "fullName": "Arindam"
}

Write a Java class this JSON can be mappend to. 编写一个可以将此JSON映射到的Java类。

public class UserConfig {
    private String user;
    private String fullName;
    // Constructor, Getter, Setter, ...
}

Then JAX-RS allows you to automatically convert the JSON to a class instance. 然后,JAX-RS允许您自动将JSON转换为类实例。

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String sayJSONHello2(UserConfig userConfig) {
  System.out.println("got UserConfig: " + userConfig);  
  return "{\"Name\":\"Arindam\"}";
}

It is also possible to let JAX-RS handle the mapping of a result to JSON. 也可以让JAX-RS处理结果到JSON的映射。 Let's write a second class for the response. 让我们为响应编写第二个类。

public class HelloResponse {
    private String name;
    // Constructor, Getter, Setter, ...
}

Change your JAX-RS method to return an instance of this class. 更改您的JAX-RS方法以返回此类的实例。

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public HelloResponse sayJSONHello2(UserConfig userConfig) {
  System.out.println("got UserConfig: " + userConfig);  
  return new HelloResponse(userConfig.getName());
}

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

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