有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Post json接收参数为空

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

这真的很好,我可以很容易地提供GET服务,甚至是POST服务。然而,在POST方法中,我收到一个空对象。这是我的

在我的豆子里。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"/>

这是我的个人资料

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);

    }

现在我的建议是:

        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非常空:)

我用的是邮递员,当我做GET时,它工作得很好。对于post,我收到一个空对象

    {
  "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);

当我调试我接收的对象时,它是空的,这对于创建一个帐户来说非常重要:) 我搜索了很多东西,没有问任何问题就达到了这一点,但现在我被无缘无故地屏蔽了几个小时:(也许你会从全球的角度告诉我,我在某个地方做了一件糟糕的事情。至于那篇帖子,我没有找到任何图图,我做了一切,就像我们用法语说的那样,“塔顿”,意思是“摸索前进”

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


共 (1) 个答案

  1. # 1 楼答案

    没关系。我找到了解决办法。 事实上,我删除了这个参数,并通过删除大写的第一个字母来更改json,只获取CreateAccountRQ中的内容