利用jersey发送post请求

依赖

springboot的版本是1.5.10

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jersey</artifactId>
</dependency>

代码实现

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.example.demo.User;

public static String post(String uri ,Form form) {
		Response response = null;
		try {
			Client client = ClientBuilder.newClient();
			WebTarget target = client.target(serverURI);
			response = target.request().buildPost(Entity.form(form)).invoke();
			String result = response.readEntity(String.class);
			System.out.println("result:"+result);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}finally {
			if(response!= null) {
				response.close();
			}			
		}
	}

猜你喜欢

转载自my.oschina.net/u/3574106/blog/1799427
今日推荐