简体   繁体   English

错误400错误的请求

[英]Error 400 Bad Request

I have some problem with making a good 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();
}

From here I pass the value! 从这里我传递了价值!

@Test
public void main() {

    ActivityClient client = new ActivityClient();

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

    System.out.println(activity);

    assertNotNull(activity);
}

Here I make the check of response.. 在这里,我检查响应。

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);

}

I got an exception from get method. 我从get方法获得异常。 The response is 400 Bad request. 响应为400错误请求。 What did I make wrong? 我做错了什么?

Here is mine web.xml file! 这是我的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:

Here is what I got from mine postman. 这是我从我的邮递员那里得到的。 I will appreciate if anybody can explain me more specific about this possible solutions... 如果有人可以向我详细说明这种可能的解决方案,我将不胜感激。

Add the Accepts header to the client code. 将Accepts标头添加到客户端代码。 The Accepts header will tell the server that the client making the request is willing to take the particular Mime type. Accepts标头将告诉服务器发出请求的客户端愿意采用特定的Mime类型。 If you are expecting json, specify application/json. 如果您期望使用json,请指定application / json。

request(MediaType.APPLICATION_JSON) will only tell that the data sent to the server is json. request(MediaType.APPLICATION_JSON)只会告诉发送到服务器的数据是json。 It does not tell what you are willing to accept. 它不会告诉您您愿意接受什么。

Make the requests in browser and use some browser addons to set the headers and watch the responses. 在浏览器中发出请求,并使用一些浏览器插件来设置标题并观察响应。

Read more about the Accept header here 在此处阅读有关Accept标头的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM