【发布时间】:2016-08-06 16:51:52
【问题描述】:
我目前正在创建一个静止的应用程序(以提高我在该领域的技能)。老实说,我很久以前就在使用 jsp 和 servlet,但现在我可以看到 rest api 上的 json 是新的工作方式。所以我说我为什么不做。 首先,我正在使用我的旧应用服务器 Tomcat(我喜欢这只猫)。我按照我在http://www.lessonslab.com/building-restful-webservices-using-apache-cxf/150/中看到的内容进行了操作
真是太好了,我能够轻松地生成 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"/>
然后这是我的个人资料Cxf
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 很空:)
我正在使用 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 和我所做的一切都像我们在法语中所说的“a taton”,意思是“摸索”
非常感谢,希望您能找到解决此问题的简单方法:)
【问题讨论】:
-
所以我犯了一个错误。事实上:create accountMethod 必须是
public Response createAccount(CreateAccountRQ createAccountRQ);CreateAccountRQ 是一个带有 ! @XMLRootElement 我能够使它与一个简单的对象一起工作。但是有了这个,我似乎仍然收到空值。我继续调查
标签: java json spring web-services xsd