我正在为Rest-full WS(已用Jersey的JAX-RS开发)创建客户端。调用POST方法
Server:Weblogic 12.2.1,它使用Jersey 2.xx
我正在获得
的例外 InboundJaxrsResponse{context=ClientResponse{method=POST, uri=http://localhost:8070/Handset/api/activate, status=400, reason=Bad Request}}
而且GET方法请求在WS客户端上也能正常工作。
下面是Web服务代码:-
@GET
@Path("/verify")
@Produces(MediaType.TEXT_PLAIN)
public Response verifyRESTService(InputStream incomingData) {
String result = "RESTService Successfully started..";
// return HTTP response 200 in case of success
return Response.status(200).entity(result).build();
}
@POST
@Path("/activate")
@Consumes({MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_XML})
public MyResponse activate(final Activate model, @Context HttpServletRequest request) throws IllegalArgumentException, IllegalAccessException {
ObjectMapper m = new ObjectMapper();
Map<String,String> requestMap = m.convertValue(model, Map.class);
Map<String, String> responseMap= new HashMap<String, String>();
Status responseStatus = new Status();
try {
responseMap=service(requestMap,request);
if(responseMap.containsKey("returnCode"))
responseStatus.setReturnCode(responseMap.get("returnCode"));
if(responseMap.containsKey("errorDescription"))
responseStatus.setErrorDescription(responseMap.get("errorDescription"));
if(responseMap.containsKey("errorCode"))
responseStatus.setErrorCode(responseMap.get("errorCode"));
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MyResponse obj = new ServiceModel();
((ServiceModel)obj).setStatus(responseStatus);
return obj;
}
下面是响应类:-
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "status")
public class Status {
private String returnCode;
private String errorDescription;
private String errorCode;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "response")
public class ServiceModel implements MyResponse {
private Status status;
}
以下是使用Jersey API的POST请求的Java客户端:-
Client client = ClientBuilder.newClient();
WebTarget webTarget=null;
Activate activate = new Activate();
activate.setToken("112233445566");
activate.setEmpID("999999");
activate.setService("TIMPersonal");
webTarget = client.target("http://localhost:8070/Handset/api").path("activate");
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_XML);
Response activateServiceResponse = invocationBuilder.post(Entity.entity(activate, MediaType.APPLICATION_XML));
下面的jar曾经用来开发WS客户端。
下面的jars被用来开发Rest-full WS
模型类:-在客户端和服务器位置都创建具有以下相同结构的Activate
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Activate {
private String token;
private String empId;
private String service;
我不确定如何构造来自Java客户端的application / xml请求。 任何帮助表示赞赏。