java中的HTTP POST引发错误-未找到合适的HttpMessageConverter来将请求主体读入类型类的对象中

问题描述 投票:0回答:2

我有一个Java进程正在监听ActiveMQ消息,并且当状态为COMPLETE时,我正在调用HTTP POST,如下面的代码所示。我指的是following article,用于发送POST()。但是,我遇到了以下问题:

在Eclipse控制台中,出现以下错误:

Testing 1 - Send Http POST request
{"timestamp":"2020-05-15T01:00:59.232+0000","status":401,"error":"Unauthorized","message":"Authentication Failed : No suitable HttpMessageConverter found to read request body into object of type class com.abc.tpms.mxnf.entities.DataDeliveryAction from request with content type of application/x-www-form-urlencoded;charset=UTF-8!","path":"/CompanyServices/api/dataDeliveryActions"}

[当我使用POSTMAN时,请求工作正常,但是我必须在POSTMAN中进行以下更改(如下面的屏幕截图所示-用红色圈起来)。

1)将我的参数放在POSTMAN的Body部分中>

2)将类型更改为JSON。

enter image description here

下面我的相关代码:

// All imports goes here

@Component
public class DownloadConsumer {

    @Autowired
    private JavaMailSender javaMailSender;

    // one instance, reuse
    private final CloseableHttpClient httpClient = HttpClients.createDefault();

    // Working Code with JMS 2.0
    @JmsListener(destination = "MessageProducer")
        public void processBrokerQueues(String message) throws DaoException {



            System.out.println("Message Retrieved is:" +message);
            try {

            RequestDao requestDao = (RequestDao) context.getBean("requestDao");

            String receivedStatus = requestDao.getRequestStatus(message);

            //Before sending this message, do the check for COMPLETE or ERROR etc
            if(receivedStatus.equals("COMPLETE")) {




                /*****************************************************\
                    // START: Calling webservices

                  *******************************************************/

                 DownloadConsumer obj = new DownloadConsumer();

                    try {


                        System.out.println("Testing 1 - Send Http POST request");
                        obj.sendPost();
                    } finally {
                        obj.close();
                    }

            }
            else {





            }

            }
            catch(Throwable th){
                th.printStackTrace();   

            }

         }




    private void close() throws IOException {
        httpClient.close();
    }

    private void sendPost() throws Exception {


       HttpPost post = new HttpPost("https://myservername.com/CompanyServices/api/dataDeliveryActions");

        // add request parameter, form parameters
        List<NameValuePair> urlParameters = new ArrayList<>();
        urlParameters.add(new BasicNameValuePair("requestId", "123456"));
        urlParameters.add(new BasicNameValuePair("projectId", "71"));
        urlParameters.add(new BasicNameValuePair("assetId", "4"));
        urlParameters.add(new BasicNameValuePair("assetName", "Test at PM By User"));

        post.setEntity(new UrlEncodedFormEntity(urlParameters));

        try (CloseableHttpClient httpClient = HttpClients.createDefault();
             CloseableHttpResponse response = httpClient.execute(post)) {

            System.out.println(EntityUtils.toString(response.getEntity()));
        }

    }





    // URL of the JMS server. DEFAULT_BROKER_URL will just mean that JMS server is on localhost
    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
    private static String subject = "MessageProducer"; //Queue Name
    // default broker URL is : tcp://localhost:61616"

    private static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    private static final Logger logger = LoggerFactory.getLogger(DownloadConsumer.class);



}

[在调用HTTP POST时sendPost()方法内在做什么?我是否需要像在POSTMAN中一样照顾JSON。如果是,那怎么办?请指教。谢谢!

我有一个Java进程正在监听ActiveMQ消息,并且当状态为COMPLETE时,我正在调用HTTP POST,如下面的代码所示。我指的是下面的文章,用于发送...

java api http-post url-parameters
2个回答
0
投票

[从我的观察中,我可以理解,在您的邮递员呼叫中,您正在发送带有application/json作为content-type并在请求正文中包含json的发布请求,效果很好。但是在您的代码中,您将其设置为:


0
投票

创建一个JSON字符串以传递给您的API。然后使用您的JSON字符串和URL调用以下函数。

© www.soinside.com 2019 - 2024. All rights reserved.