Spring,Java:发送松弛消息导致500内部服务器错误

时间:2017-08-28 09:18:57

标签: java spring slack slack-api

我正在努力将Slack集成到我们的应用程序中。截至目前,为了进行测试,我试图了解如何简单地在Slack上发送消息。每当我尝试发送消息时,我都会收到500内部服务器错误..

另外,在用户通过Slack进行身份验证后,有没有办法知道Slack的通道处理程序?在我们寻找集成时,似乎我们需要服务URL来在Slack上发送消息。如何为经过身份验证的用户获取Slack的服务URL。

错误日志:

org.springframework.web.client.HttpServerErrorException: 500 Server Error
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:614)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:570)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:530)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:448)
    at com.application.spring.controller.AttachmentController.sendSlackMsg(AttachmentController.java:57)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

代码:

@RequestMapping(value = "/sendslackmsg")
    public @ResponseBody String sendSlackMsg(){
        try {
            String serviceUrl = "https://hooks.slack.com/services/XXX/XXXX/XXXXXX";
            JSONObject jsonObject = new JSONObject();
            jsonObject.append("text","hello world");
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            HttpHeaders headers = new HttpHeaders();
            headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
            HttpEntity<String> entity = new HttpEntity<>(jsonObject.toString(), headers);

            ResponseEntity<String> loginResponse = restTemplate
                    .exchange(serviceUrl, HttpMethod.POST, entity, String.class);
            System.out.println("Status code is "+loginResponse.getStatusCode().toString());
            return loginResponse.getStatusCode().toString();
        }catch (HttpStatusCodeException e){
            e.printStackTrace();
            return e.toString();
        }
    }

0 个答案:

没有答案