2016-08-06 59 views
0

我目前正在创建一个应用程序在休息(为了提高我在这个领域的技能)。很荣幸,我很早以前就开始使用jsp和servlet,但现在我可以看到json在rest api上是一种新的工作方式。所以我说了为什么我不会这样做。 首先我正在工作wuth我的老应用服务器Tomcat(我爱这只猫)。我遵循我所看到的http://www.lessonslab.com/building-restful-webservices-using-apache-cxf/150/发布json收到参数为空

这真的很好,我能够轻松地生成GET服务甚至POST服务。但是在POST方法中,我收到一个空对象。这是我的。

在我的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); 

    } 

,现在我实现了一套:

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

    } 

你可以看到,纽约IMPL是相当空:)

我是你当我正在做一个GET时,唱Postman,它工作的很好。对于这篇文章,我收到一个空对象。

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

当我调试我收到的对象为空,这是相当是烦人,以创建一个帐户:) 我搜索了很多到达在那里没有问问题,但现在我被禁止了几个小时没有:(也许你会有一个全球性的观点,说我在某个地方做了一个糟糕的事情,至于帖子,我没有找到任何tuto,我做了一切就像我们用法语“a taton”的意思是“摸索”一样

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

+0

所以我犯了一个错误。实际上: 创建帐户方法必须是 '公共响应createAccount(CreateAccountRQ createAccountRQ);' CreateAccountRQ是一个带有! @XMLRootElement 我能够使它与一个简单的对象一起工作。但有了这个,似乎我仍然收到空。我继续调查 – Geoffrey

回答

0

没问题。我找到了解决方案。 事实上,我删除了参数,并通过删除UPPER情况下的第一个字母来更改json,并且只采用了CreateAccountRQ内部的内容。