HTTP 500错误失败,状态代码为200

时间:2014-10-07 06:31:20

标签: java rest wso2 jax-rs

我开发了一个jax-rs项目,并调用REST服务将数据添加到数据库。当我发送POST请求以添加一些数据时,它会提供200个状态代码,将数据添加到数据库中,此外它还会显示500状态,表示内部服务器错误。

下面是我的jax-rs代码。

@POST
@Path("/users/add/")
@Produces("text/plain")
public Response addUser(
        @QueryParam("userId") String userId,
        @QueryParam("userName") String userName,
        @QueryParam("userRole") String userRole,
        @QueryParam("email") String email,
        @QueryParam("password") String password,
        @QueryParam("phone") String phone) {

    System.out.println("----invoking addUser");

    try{
        httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost("http://appserver.dev.cloud.wso2.com/services/t/madusanka/projecttrackerdb-default-SNAPSHOT/addUser");
        postRequest.addHeader("accept", "application/xml");

        List nameValuePairs = new ArrayList();
        nameValuePairs.add(new BasicNameValuePair("userId",userId));
        nameValuePairs.add(new BasicNameValuePair("userName",userName));
        nameValuePairs.add(new BasicNameValuePair("userRole",userRole));
        nameValuePairs.add(new BasicNameValuePair("email",email));
        nameValuePairs.add(new BasicNameValuePair("password",password));
        nameValuePairs.add(new BasicNameValuePair("phone",phone));
        postRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));


        HttpResponse response = httpClient.execute(postRequest);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != 201)
        {
            throw new RuntimeException("Failed with HTTP error code : " + statusCode);
        }
    }catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally{
        //Important: Close the connect
        httpClient.getConnectionManager().shutdown();
    }
    return Response.ok().build();
}

当我从REST客户端拨打电话时收到以下回复。

Status : 500 Internal Server Error
org.apache.cxf.interceptor.Fault: Failed with HTTP error code : 200
org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:162)
org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:128)
org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:194)
org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:100)
org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:57)
org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:93)
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:239)
org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:223)
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:203)
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:137)
org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:159)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:286)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:206)
javax.servlet.http.HttpServlet.service(HttpServlet.java:755)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:262)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:274)
org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:271)
java.security.AccessController.doPrivileged(Native Method)
javax.security.auth.Subject.doAsPrivileged(Subject.java:536)
org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:306)
org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:166)

这可能是什么原因?

1 个答案:

答案 0 :(得分:1)

您使用201检查状态。您可以将其更改为200

    if (statusCode != 200)
    {
        throw new RuntimeException("Failed with HTTP error code : " + statusCode);
    }

最好使用rang而不是一个数字来检查HTTP失败:

    if (statusCode > 399)
    {
        throw new RuntimeException("Failed with HTTP error code : " + statusCode);
    }