我正在编写一个Restful webservice,它打算接受并返回一个json
我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>cxf</display-name>
<servlet>
<servlet-name>cxf</servlet-name>
<description>Apache CXF Endpoint</description>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
我的cxf-servlet.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:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
<import resource="/applicationContext.xml" />
<jaxrs:server id="isAdaptor" address="/crossover">
<jaxrs:serviceBeans>
<ref bean="salesResource" />
</jaxrs:serviceBeans>
</jaxrs:server>
<bean id="salesResource" class="com.crossover.resource.SalesResource" />
</beans>
我的SalesResource
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import com.crossover.dao.SalesOrderDao;
import com.crossover.model.OrderDTO;
import com.crossover.model.StatusDTO;
@Path("/sales")
public class SalesResource {
@Autowired
private SalesOrderDao salesorderDao;
@POST
@Path("/processSalesOrder")
@Produces("application/json")
@Consumes("application/json")
public Response processSalesOrder(OrderDTO orderDto) {
System.out.println("inside processSalesOrder " + orderDto);
ResponseBuilder responseBuilder = Response.status(Response.Status.OK);
StatusDTO smdto = new StatusDTO("200", "SUCCESS");
return responseBuilder.build();
}
}
OrderDTO.java
public class OrderDTO {
private String customerCode;
private String productCode;
private int requiredQty;
public String getCustomerCode() {
return customerCode;
}
public void setCustomerCode(String customerCode) {
this.customerCode = customerCode;
}
public String getProductCode() {
return productCode;
}
public void setProductCode(String productCode) {
this.productCode = productCode;
}
public int getRequiredQty() {
return requiredQty;
}
public void setRequiredQty(int requiredQty) {
this.requiredQty = requiredQty;
}
@Override
public String toString() {
return "OrderDTO [customerCode=" + customerCode + ", productCode="
+ productCode + ", requiredQty=" + requiredQty + "]";
}
}
我的客户
public class RestClient {
public static void main(String[] args) {
try {
URL url = new URL(
"http://localhost:8080/crossover-1.0.0/crossover/sales/processSalesOrder");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
String input = "{\"customerCode\":\"customerCode\",\"productCode\":\"productCode\",\"requiredQty\":10}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
下面的是我的例外
10:31:59.346 [http-bio-8080-exec-1] DEBUG o.a.c.j.i.JAXRSInInterceptor - Accept contentType is: */*
10:31:59.348 [http-bio-8080-exec-1] DEBUG o.a.c.j.i.JAXRSInInterceptor - Found operation: processSalesOrder
10:31:59.357 [http-bio-8080-exec-1] WARN o.apache.cxf.jaxrs.utils.JAXRSUtils - No message body reader has been found for request class OrderDTO, ContentType : application/json.
10:31:59.390 [http-bio-8080-exec-1] WARN o.a.c.j.i.WebApplicationExceptionMapper - javax.ws.rs.WebApplicationException
at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody(JAXRSUtils.java:1261)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameter(JAXRSUtils.java:790)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameters(JAXRSUtils.java:749)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:236)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:101)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:239)
at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:223)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:203)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:137)
at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:159)
我的输入json是
{
"customerCode":"customerCode",
"productCode":"productCode",
"requiredQty":10
}