[英]Post json receive null for parameter
我目前正在創建一個處於靜止狀態的應用程序(以提高我在該領域的技能)。 坦白地說,我很久以前就使用過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"/>
然后這是我的個人資料
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,我做了一切,就像我們用法語“ taton”說的那樣,意思是“摸索着”
在此先多謝,希望您能找到一個解決此問題的簡單方法:)
沒關系。 我找到了解決方案。 實際上,我刪除了參數,並通過刪除大寫字母中的第一個字母來更改了json,並且僅使用了CreateAccountRQ內的內容
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.