簡體   English   中英

錯誤400錯誤的請求

[英]Error 400 Bad Request

我提出一個好的要求有些問題。

@Path("/activitiesGenerator")
public class ActivityResource {

ActivityStub acstub = new ActivityStub();

@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Collection<Activity> getAllActivities(){

    return acstub.Find_All_Activities();
}


@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("{activityId}") // Will act as parameter that will take value from the browser!
public Response getActivity(@PathParam("activityId") String activityId){

    if(activityId.equals(null) || activityId.length() < 4){

        return Response.status(Status.BAD_REQUEST).build();
    }

    Activity activity  = acstub.FindActivity(activityId);

    if(activity.equals(null)){

        return Response.status(Status.NOT_FOUND).build();
    }

    return Response.ok().entity(activity).build();
}

從這里我傳遞了價值!

@Test
public void main() {

    ActivityClient client = new ActivityClient();

    Activity activity = client.get("3204987");

    System.out.println(activity);

    assertNotNull(activity);
}

在這里,我檢查響應。

public Activity get(String id){

    WebTarget target = client.target("http://localhost:8080/Activities/rest/");
    Response response = target.path("activitiesGenerator/"+id).request(MediaType.APPLICATION_JSON).get(Response.class);

    System.out.println(response.getStatus());

    if(response.getStatus() != 200){

        throw new RuntimeException(response.getStatus()+ ": there was an error on the server");
    }

    return response.readEntity(Activity.class);

}

我從get方法獲得異常。 響應為400錯誤請求。 我做錯了什么?

這是我的web.xml文件!

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">

    <display-name>Activities</display-name>
    <servlet>
        <servlet-name>ActiviteExample</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>activity.model</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>ActiviteExample</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

 Could not get any response
 This seems to be like an error connecting to      http://HTTP://localhost:8080/Activities/rest/activities-generator/3204987.
 Why this might have happened:
 The server couldn't send a response:
 Ensure that the backend is working properly
 SSL connections are being blocked:
 Fix this by importing SSL certificates in Chrome
 Cookies not being sent:

這是我從我的郵遞員那里得到的。 如果有人可以向我詳細說明這種可能的解決方案,我將不勝感激。

將Accepts標頭添加到客戶端代碼。 Accepts標頭將告訴服務器發出請求的客戶端願意采用特定的Mime類型。 如果您期望使用json,請指定application / json。

request(MediaType.APPLICATION_JSON)只會告訴發送到服務器的數據是json。 它不會告訴您您願意接受什么。

在瀏覽器中發出請求,並使用一些瀏覽器插件來設置標題並觀察響應。

在此處閱讀有關Accept標頭的更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM