gpt4 book ai didi

java - 错误 400 错误请求

转载 作者:行者123 更新时间:2023-12-01 10:01:13 24 4
gpt4 key购买 nike

我在提出好的请求时遇到一些问题。

@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:

这是我从 postman 那里得到的信息。如果有人能更具体地向我解释这个可能的解决方案,我将不胜感激......

最佳答案

将 Accepts header 添加到客户端代码中。 Accepts header 将告诉服务器发出请求的客户端愿意采用特定的 Mime 类型。如果您需要 json,请指定 application/json。

request(MediaType.APPLICATION_JSON)只会告诉发送到服务器的数据是json。它并没有告诉你你愿意接受什么。

在浏览器中发出请求并使用一些浏览器插件来设置 header 并观察响应。

了解有关 Accept header here 的更多信息

关于java - 错误 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36789645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com