简体   繁体   中英

Can't get json parameter to Jersey @POST / GET

We are using dynamic web app with jersey, the client is android mobile using phonegap jQuery and Javascript.

When the user is sending a post to the web server, it is getting the request but doesn't see any parameters (the @Counsume).

I know that the jersey is working because when I tried to use @get method on other function it worked fine.

This is my code:

//web server

 package il.ac.hit;
    import java.util.Map;
    import javax.ws.rs.*;
    @Path("/TakeAHikeDao")
    public class TakeAHikeDao
    {
        @POST
        @Path("userCredentialsValidate")
        @Consumes("application/json")
        @Produces("application/text")
        public String userCredentialsValidate(Map<String, String> userCredentials)
        {
            System.out.println(userCredentials.toString()+"%%%");
            return ("{\"status\":\"success\"}");    
        }
    }

//web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>TakeAHikeServerSide</display-name>
  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>il.ac.hit</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping> 
</web-app>

//client html with javascript code

<script language="JavaScript" charset="utf-8>
function checkUser() 
{

var url=localhost:8080/rest/TakeAHikeDao/userCredentialsValidate

$.post("url",{"username":"dan","password":"123456"},function( data ) {
        alert( "JSON Data: " + data);
        });

/*  this is the second option we tried
    $.get("url",{"username":"dan","password":"123456"},function( data ) {
        alert( "JSON Data: " + data);
        });
*/

 /* this is the third option we tried       
    $.getJSON("url",{"username":"dan","password":"123456"},function( data ) {
            alert( "JSON Data: " + data);
            });
 */
}
</script>

//this is the button calling the function.
<a href="#" data-role="button" data-icon="arrow-r" onClick="checkUser()">SUBMIT</a>

The output that I recieved in the console from the the method:

System.out.println(userCredentials.toString()+"%%%");

is "%%%" (the userCredentials is empty and not getting the data that was send).

Thanks in advance, ofir.

您是否尝试过更改方法签名以采用强类型参数名称而不是通用参数?

I'm happy to say that i've found the problem, The Changes are on the consumes:

@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
MultiValuedMap<String, String> userCredentials

@POST
@Path("userCredentialsValidate")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)   
public String userCredentialsValidate(MultiValuedMap<String, String> userCredentials)
{
        System.out.println(userCredentials.toString()+"%%%");
        return ("{\"status\":\"success\"}");
}

i Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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