post json接收参数

时间:2016-08-06 16:51:52

标签: java json spring web-services xsd

我目前正在创建一个静态的应用程序(为了提高我在这个领域的技能)。值得称道的是,我很久以前就在使用jsp和servlet,但现在我可以看到休息api上的json是新的工作方式。所以我说为什么我不会这样做。 首先,我正在使用我的旧应用服务器Tomcat(我喜欢这只猫)。我遵循了我在http://www.lessonslab.com/building-restful-webservices-using-apache-cxf/150/

中看到的内容

非常好,我能够轻松地制作GET服务甚至POST服务。但是在POST方法中,我收到一个null对象。这就是我所拥有的。

在我的beans.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">

<import resource="classpath:META-INF/cxf/cxf.xml" />
<context:component-scan base-package="com.bdproject.*" />

<jaxrs:server id="createAccount" address="/createAccount">
    <jaxrs:providers>
        <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
    </jaxrs:providers>
    <jaxrs:serviceBeans>
        <ref bean="ProfileCXFServiceImpl" />
    </jaxrs:serviceBeans>
    <jaxrs:extensionMappings>
        <entry key="xml" value="application/xml" />
        <entry key="json" value="application/json" />
    </jaxrs:extensionMappings>
</jaxrs:server>

<bean id="ProfileCXFServiceImpl" class="com.bdproject.cxfrestservice.ProfileCXFServiceImpl"/>

然后这是我的profileCxf

package com.bdproject.cxfrestservice;

    import javax.jws.WebService;
    import javax.ws.rs.Consumes;
    import javax.ws.rs.POST;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.QueryParam;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Request;
    import javax.ws.rs.core.Response;

    /**
     *
     * @author lessonslab.com
     * This is interface for the employee services
     *
     */
    @Path("/")
    @WebService(name="createAccount", targetNamespace="")
    public interface ProfileCXFService
    {

      @POST
      @Path("/createAccount")
      @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
      @Consumes(MediaType.APPLICATION_JSON)
      public Response createAccount(@QueryParam("CreateAccountRQ") Request createAccountRQ);

    }

现在我的impl:

        package com.bdproject.cxfrestservice;

    import javax.ws.rs.core.Request;
    import javax.ws.rs.core.Response;

    public class ProfileCXFServiceImpl implements ProfileCXFService{

      @Override
      public Response createAccount( Request createAccountRQ) {
        // TODO Auto-generated method stub
        return null;
      }

    }

你可以看到ny impl非常空:)

我正在使用Postman,当我正在进行GET时效果很好。对于帖子,我收到一个空对象。

    {
  "CreateAccountRQ": {
    "-xsi:noNamespaceSchemaLocation": "schema.xsd",
    "-xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
    "ProfileInformation": {
      "-ProfileID": "String",
      "-Name": "String",
      "-Surname": "text",
      "Personnal": {
        "Contacts": {
          "Contact": [
            {
              "-Type": "String",
              "-Name": "String",
              "PhoneContact": {
                "Description": {
                  "-Value": "4545454554",
                  "-AreaCode": "06",
                  "-OverseaCode": "+33"
                }
              }
            }
          ]
        }
      }
    },
    "AccountInformation": {
      "emailAddress": "String",
      "Password": "String"
    }
  }
}

我尝试过很多东西:

    @POST
@Path("/createAccount")
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
@Consumes(MediaType.APPLICATION_JSON)
public Response createAccount(@PathParam("CreateAccountRQ") Request createAccountRQ);

当我调试我收到的对象是null时,创建一个帐户非常烦人:) 我搜索了很多以达到那一点而没有提出问题,但现在我被封锁了几个小时没有任何东西:(也许你会有一个全球观点来说我在某个地方做了一个糟糕的事情,因为我没找到的帖子任何tuto和我做的就像我们用法语说的那样#34;一个taton&#34;意思&#34;摸索&#34;

非常感谢,我希望您能找到解决此问题的简单方法:)

1 个答案:

答案 0 :(得分:0)

没关系。我找到了解决方案。 实际上我删除了参数并通过删除UPPER中的第一个字母来更改了json,并且仅使用了CreateAccountRQ中的内容