Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 理解REST创建请求url_Java_Spring_Rest - Fatal编程技术网

Java 理解REST创建请求url

Java 理解REST创建请求url,java,spring,rest,Java,Spring,Rest,通过rest url创建实体(比如myEntity)的url是什么myEntity有两个参数name和description以下是rest控制器的外观: @POST @Path("/create") @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public Respo

通过rest url创建实体(比如
myEntity
)的url是什么
myEntity
有两个参数
name
description

以下是rest控制器的外观:

@POST
@Path("/create")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response createJobType(MyEntity myEntity) {}
如果看起来正常,那么
myEntity
参数将如何通过请求url传递?
这是一个测试类:

@Test
public void testShouldCreateMyEntity() {
    MyEntity entity = new MyEntity();
    entity.setName("Sample Name");
    entity.setDescription("Sample Description);
    String url = buildRequestURL("/create/"+entity).toUrl(); // Confused :(
}

不确定是否应通过URL传递实体。如果没有,那么实体将如何通过?

测试端点的方法有很多,这些测试可能会根据您的需要而有所不同

例如,如果需要身份验证,或者如果需要HTTPS

但是,假设您不需要身份验证且未使用HTTPS,则可以使用以下代码测试您的端点:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

import com.google.gson.Gson;

public class RestClientTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        CloseableHttpClient httpClient = null;
        HttpPost httpPost = null;
        CloseableHttpResponse response = null;

        try {

            httpClient = HttpClients.createDefault();
            httpPost = new HttpPost("http://localaddressportetc.../create"); // <-- I suggest you change this to "entity" since this is what is being created by the POST

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("content-type", "application/json"));

            MyEntity entity = new MyEntity();
            entity.setName("Sample Name");
            entity.setDescription("Sample Description");

            Gson gson = new Gson();
            String entityJSON = gson.toJson(entity);

            StringEntity input = new StringEntity(entityJSON);
            input.setContentType("application/json");
            httpPost.setEntity(input);

            for (NameValuePair h : nvps)
            {
                httpPost.addHeader(h.getName(), h.getValue());
            }

            response = httpClient.execute(httpPost);

            if (response.getStatusLine().getStatusCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + response.getStatusLine().getStatusCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (response.getEntity().getContent())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }

        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        } finally {
            try{
                response.close();
                httpClient.close();
            }catch(Exception ex) {
                ex.printStackTrace();
            }
        }

    }
}
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.net.MalformedURLException;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.methods.CloseableHttpResponse;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.entity.StringEntity;
导入org.apache.http.impl.client.CloseableHttpClient;
导入org.apache.http.impl.client.HttpClients;
导入org.apache.http.message.BasicNameValuePair;
导入com.google.gson.gson;
公共类RestClientTest{
/**
*@param args
*/
公共静态void main(字符串[]args){
CloseableHttpClient httpClient=null;
HttpPost HttpPost=null;
CloseableHttpResponse响应=null;
试一试{
httpClient=HttpClients.createDefault();

httpPost=新的httpPost(“http://localaddressportetc.../create");//在正文中传递参数,如表单参数、JSON或其他内容类型。请注意,由于这是REST,URL不应是动词。操作应由HTTP方法描述。创建可以是POST或PUT。您应该发送POST请求。POST请求通常在请求正文中传递参数,而不是在查询字符串中。您可以n传递XML或JSON格式的实体(我更喜欢使用JSON)。@SotiriosDelimanolis谢谢。是的,我有创建方法作为
POST
。url
create
代表动词吗?是的,create是动词。
/entity
在REST中更有意义。非常感谢。非常感谢。