我在提出好请求方面遇到了一些问题。
@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 Bad request。 我做错了什么?
这是我的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:
这是我从邮差中得到的。如果有人能够更具体地解释这个可能的解决方案,我将不胜感激......
答案 0 :(得分:1)
将Accepts标头添加到客户端代码中。 Accepts标头将告诉服务器发出请求的客户端是否愿意采用特定的Mime类型。如果您期望json,请指定application / json。
request(MediaType.APPLICATION_JSON)
只会告诉发送到服务器的数据是json。它没有告诉你愿意接受什么。
在浏览器中发出请求并使用一些浏览器插件来设置标头并观察响应。