jersey client 傳送Post請求 【帶引數】
阿新 • • 發佈:2018-11-10
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
[java] view plain copy print ?- @Path("postUser"
- public class PostUser {
- @POST
- @Produces(MediaType.TEXT_XML)
- public String postUser(){
- return "濤哥";
- }
- @Path("bean")
- @POST
- @Consumes
- @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
- public User postUser(@FormParam("username") String username){
- User user = new User();
- System.out.println(username);
- user.setUsername(username);
- user.setPassword("濤哥");
- return user;
- }
- }
[java] view plain copy print ?
- public class PostUser {
- /**
- * @param args
- * @throws UnsupportedEncodingException
- */
- public static void main(String[] args) throws UnsupportedEncodingException {
- ClientConfig config = new DefaultClientConfig();
- Client client = Client.create(config);
- WebResource service = client.resource(getBaseURI());
- MultivaluedMap<String, String> param = new MultivaluedMapImpl();
- param.add("username", "ssss");
- System.out.println(service.path("services").path("postUser")
- .path("bean").queryParams(param)
- .type(MediaType.APPLICATION_FORM_URLENCODED).post(String.class));
- }
- private static URI getBaseURI() {
- return UriBuilder.fromUri(
- "http://localhost:8080/jersey/").build();
- }
// 注意 .type(MediaType.APPLICATION_FORM_URLENCODED).
type 而不是 accept 否則將出錯的