日期:2014-05-17 浏览次数:20725 次
@Test public void testSaveOrUpdate(){ String url = "http://localhost:8081/services/userservices/adduser"; HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod(url); postMethod.setRequestHeader("Content-type","application/xml"); postMethod.setRequestHeader("user", "vis"); postMethod.setRequestHeader("pass", "vis@amoy"); postMethod.setRequestEntity(new InputStreamRequestEntity(this.getClass().getResourceAsStream("testAddUsers.xml"))); try{ int status = httpClient.executeMethod(postMethod); if(status == HttpStatus.SC_OK){ System.out.println(new String(postMethod.getResponseBody(), "utf-8")); } }catch (HttpException e) { throw new RuntimeException("",e); } catch (IOException e) { throw new RuntimeException("",e); }finally{ postMethod.releaseConnection(); } }
@Path("/userservices/") @Produces("application/xml") public class UserService { @Context private org.apache.cxf.jaxrs.ext.MessageContext messageContext; @POST @Path("/adduser") @Consumes("application/xml") public Response saveOrUpdate(VisUsers users) { if(Authentication.validate(messageContext)){ int result=userDao.saveOrUpdate(users); if(result>0){ return Response.status(200).entity("Save OK!").build(); }else{ return Response.status(401).entity("insert user error!").build(); } }else{ return Response.status(401).build(); } }
<bean id="userservices" class="com.sns.bestv.service.impl.UserService"> <property name="userDao" ref="userDao"></property> </bean> <jaxrs:server id="mmberService_wa" address="/"> <jaxrs:serviceBeans> <ref bean="mmberService" /> <ref bean="userservices" /> </jaxrs:serviceBeans> </jaxrs:server>