404 код ошибки из службы после перезапуска с использованием HttpURLConnection - PullRequest
0 голосов
/ 17 июня 2019

Я схожу с ума от этого кода. Успешно или нет, запускаю ли я код как Java-приложение в классе, у которого есть метод main, или на сервере Tomcat.

public class Test {

    public static void main(String[] args) {
        try { 

            String userCredentials = "USR28:YG739G5XFVPYYV4ADJVW";
            String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));

            URL url = new URL("http://74.208.84.251:8221/QosicBridge/user/deposit"); 
            HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
            conn.setDoOutput(true); conn.setRequestMethod("POST"); 
            conn.setRequestProperty("Content-Type", "application/json"); 
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty ("Authorization", basicAuth);

            String input = "{\"msisdn\":\"22997858711\",\"amount\":500,\"transref\":\"CZ21FRY3XDPGDBX7L745\",\"clientid\":\"UBHQ\"}";
            System.out.println(input); 

            OutputStream os = conn.getOutputStream(); 
            os.write(input.getBytes()); 
            os.flush(); 

            if (conn.getResponseCode() != 200) { 
                throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); 
            } 

            BufferedReader br = new BufferedReader(new InputStreamReader( (conn.getInputStream()))); 
            String output; 
            System.out.println("Output from Server .... \n"); 

            while ((output = br.readLine()) != null) { 
                System.out.println(output); 
            } conn.disconnect(); 
        } catch (MalformedURLException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        }
    }

}

И ошибка при запуске кода на сервере tomcat

Мне нужна помощь в устранении неполадок. Найдите ниже сообщение об ошибке

Большое спасибо

17 июля 2019 г. 16:25:48 org.apache.catalina.core.StandardWrapperValve вызвать GRAVE: Servlet.service () du Servlet [весна] au chemin [/ padme] возвращение исключений [обработка запросов не удалось; Вложенное исключение - java.lang.RuntimeException: Ошибка: HTTP код ошибки: 404] avec la reason java.lang.RuntimeException: Failed: Код ошибки HTTP: 404 при com.controller.MakeDepositController.postTransferToMerchantAccount (MakeDepositController.java:103) в sun.reflect.NativeMethodAccessorImpl.invoke0 (собственный метод) в sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) в sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) в java.lang.reflect.Method.invoke (Method.java:498) в org.springframework.web.method.support.InvocableHandlerMethod.doInvoke (InvocableHandlerMethod.java:221) в org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest (InvocableHandlerMethod.java:137) в org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle (ServletInvocableHandlerMethod.java:110) в org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod (RequestMappingHandlerAdapter.java:777) в org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal (RequestMappingHandlerAdapter.java:706) в org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle (AbstractHandlerMethodAdapter.java:85) в org.springframework.web.servlet.DispatcherServlet.doDispatch (DispatcherServlet.java:943) в org.springframework.web.servlet.DispatcherServlet.doService (DispatcherServlet.java:877) в org.springframework.web.servlet.FrameworkServlet.processRequest (FrameworkServlet.java:966) в org.springframework.web.servlet.FrameworkServlet.doPost (FrameworkServlet.java:868) в javax.servlet.http.HttpServlet.service (HttpServlet.java:660) в org.springframework.web.servlet.FrameworkServlet.service (FrameworkServlet.java:842) в javax.servlet.http.HttpServlet.service (HttpServlet.java:741) в org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java:231) в org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:166) в org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:53) в org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java:193) в org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:166) в org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy.java:316) в org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke (FilterSecurityInterceptor.java:126) в org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter (FilterSecurityInterceptor.java:90) в org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy.java:330) в org.springframework.security.web.access.ExceptionTranslationFilter.doFilter (ExceptionTranslationFilter.java:114) в org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy.java:330) в org.springframework.security.web.session.SessionManagementFilter.doFilter (SessionManagementFilter.java:122) вorg.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy.java:330) в org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter.doFjwork.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy.java:330) по адресу org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter (SecurityContextHolderAwareRequestFilil.haha.FFFF)Java: 330) в org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter (RequestCacheAwareFilter.java:48) в org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.jaf.prog.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal (BasicAuthenticationFilter.java:158) в org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter.java:107) на org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy.java:330) на org.springframework.security.205) по адресу org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy.java:330) по адресу org.springframework.security.web.authentication.logout.LogoutFilter.doFilter (Logout.Frater в фрейме org).security.web.FilterChainProxy.(OncePerRequestFilter.java:107) в org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy.java:330) в org.springframework.security.web.header.HeternalWilFerWriterFilter..security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal (WebAsyncManagerIntegrationFilter.java:53) в org.springframework.web.filter.OncePerRequestFilter.doFilter:$ VirtualFilterChain.doFilter (FilterChainProxy.java:330) в org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter (SecurityContextPersistenceFilter.java:91) в org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy.java: 330) в org.springframework.security.web.FilterChainProxy.doFilterInternal (FilterChainProxy.java:213) в org.springframework.security.web.FilterChainProxy.doFilter (FilterChainProxy.java:176).internalDoFilter (ApplicationFilterChain.java:193) в org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:166) в org.apache.catalina.core.StandardWrapperValve.invoke (стандартный ящер.catalina.core.StandardContextValve.invoke (StandardContextValve.java:96) в org.apache.catalina.authenticator.AuthenticatorBase.invoke (AuthenticatorBase.java:490) в org.apache.catalina.core.SostndinVost: 139) вorg.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:92) в org.apache.catalina.valves.AbstractAccessLogValve.invoke (AbstractAccessLogValve.java:678) в org.apache.catalina.core.StandardEngineValve.invoke (StandardEngineValve.java:74) в org.apache.catalina.connector.CoyoteAdapter.service (CoyoteAdapter.java:343) в org.apache.coyote.http11.Http11Processor.service (Http11Processor.java:408) в org.apache.coyote.AbstractProcessorLight.process (AbstractProcessorLight.java:66) в org.apache.coyote.AbstractProtocol $ ConnectionHandler.process (AbstractProtocol.java:836) в org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun (NioEndpoint.java:1839) в org.apache.tomcat.util.net.SocketProcessorBase.run (SocketProcessorBase.java:49) в java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1149) в java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:624) в org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run (TaskThread.java:61) at java.lang.Thread.run (Thread.java:748)

1 Ответ

0 голосов
/ 18 июня 2019

Я думаю, что ошибка происходит из-за объекта json, переданного в запрос.Когда я использую строковый объект (directInput в комментарии), он работает нормально.Но когда я использую объект Json (jsonInput), он дает мне код ошибки 404.Кто-нибудь может помочь в устранении неполадок?

Спасибо

public class Test {

public static void main(String[] args) {
    try { 

        String userCredentials = "USR28:YG739G5XFVPYYV4ADJVW";
        String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));

        URL url = new URL("http://74.208.84.251:8221/QosicBridge/user/deposit"); 
        HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST"); 
        conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty ("Authorization", basicAuth);

        //String directInput = "{\"msisdn\":\"22997858711\",\"amount\":1400,\"transref\":\"JDQY78J6ABX68F0T56FP\",\"clientid\":\"UBHQ\"}";

        DepositObject d = new DepositObject();
        d.setMsisdn("22997858711");
        d.setAmount(1400);
        d.setTransref("JDQY78J6ABX68F0T56FP");
        d.setClientid("UHBQ");

        Gson gson = new Gson();
        String jsonInput = gson.toJson(d).toString();

        OutputStream os = conn.getOutputStream();
        os.write(jsonInput.getBytes()); 
        os.flush(); 

        if (conn.getResponseCode() != 200) { 
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); 
        } 

        BufferedReader br = new BufferedReader(new InputStreamReader( (conn.getInputStream()))); 
        String output; 
        while ((output = br.readLine()) != null) { 
            System.out.println(output); 
        } 

        conn.disconnect();

    } catch (MalformedURLException e) { 
        e.printStackTrace(); 
    } catch (IOException e) { 
        e.printStackTrace(); 
    }
}

}

...