![](/img/trans.png)
[英]JAX-RS Jersey JSON GET HTTP Status 500 Internal Server Error
[英]HTTP 500 Internal Server Error using Jersey
我正在使用澤西島。 因此,當我嘗試從我的服務獲取 JSON 對象響應(僅使用 JSON)時遇到問題。 我正在使用一個簡單的 apache localhost 來托管我的服務。
服務
@Path("/profile")
public class ProfileDataService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public ProfileBean getProfileData() {
ProfileBean profile = fillProfileInstance();
return profile;
}
}
網頁.xml
<web-app ...>
<display-name>cinemak-service</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.cinemak.service.services</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
客戶
public static void main(String[] args) {
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target(getURIBase());
System.out.println(target.path("rest").path("profile").request().accept(MediaType.APPLICATION_JSON).get(String.class));
}
//Build and returns the specific uri base for cinemak-service
private static URI getURIBase() {
return UriBuilder.fromUri("http://localhost:8080/cinemak-service").build();
}
我得到的錯誤:
Exception in thread "main" javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1002)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:799)
at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:91)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:687)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:683)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:411)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:307)
at com.cinemak.client.ProfileDataClient.main(ProfileDataClient.java:21)
看起來你的服務器沒有響應......我猜你的客戶端java代碼在不同的java項目中,對吧? 如果沒有,如果您的客戶端 java 代碼在同一個 java 項目中,您必須啟動您的應用程序服務器,之后,您可以運行您的客戶端類的 main 方法。 如果你的客戶端java代碼在其他java項目中,是一樣的,你必須啟動你的服務器才能調用RestFul路徑。 順便說一句,您的 Restful 應用程序項目名稱是小寫的“cinemak-service”? 只是為了確保這不是一個錯字。
波紋管最糟糕的編碼你會得到HTTP 500 內部服務器錯誤使用 Jersey。
- 這些示例中的兩個方法產生和使用完全相同的 MIME 類型,因此它們作為資源方法的調用將
總是失敗。
package rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/HelloService")
public class HelloService {
@GET
@Path("/{param}/{param2}")
public Response getMsg(@PathParam("param") String msg,
@PathParam("param2") String msg2) { // from path
String output = "Jersey say : " + msg + "/" + msg2;
return Response.status(200).entity(output).build();
}
@GET
@Path("/{param}/{param2}")
public Response getMsg2(@PathParam("param") String msg,
@PathParam("param2") String msg2) { // from path
String output = "Jersey say-2 : " + msg + "/" + msg2;
return Response.status(200).entity(output).build();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.