我正在尝试使用 XML 数据处理请求,出现错误
RecurlyController.processEvents(java.lang.Object,javax.servlet.http.HttpServletRequest)]:
org.springframework.web.HttpMediaTypeNotSupportedException:
Controller
我想要任何结构的 XML 数据,这就是我在请求中使用对象的原因
@RequestMapping(value = "events", method = RequestMethod.POST, consumes = MediaType.APPLICATION_XML_VALUE)
public ModelAndView processEvents(@RequestBody Object recurlyEvent, HttpServletRequest request {
Response response = recurlyManager.processEvents(recurlyEvent);
return prepareModelAndView(response);
}
使用 postman 向 Spring 应用程序发出 Http 请求
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/xml");
RequestBody body = RequestBody.create(mediaType, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<new_subscription_notification>\n <account>\n <account_code>1</account_code>\n <username nil=\"true\">verena</username>\n <email>verena@example.com</email>\n <first_name>Verena</first_name>\n <last_name>Example</last_name>\n <company_name nil=\"true\">Company, Inc.</company_name>\n </account>\n <subscription>\n <plan>\n <plan_code>bronze</plan_code>\n <name>Bronze Plan</name>\n </plan>\n <uuid>8047cb4fd5f874b14d713d785436ebd3</uuid>\n <state>active</state>\n <quantity type=\"integer\">2</quantity>\n <total_amount_in_cents type=\"integer\">17000</total_amount_in_cents>\n <subscription_add_ons type=\"array\">\n <subscription_add_on>\n <add_on_code>premium_support</add_on_code>\n <name>Premium Support</name>\n <quantity type=\"integer\">1</quantity>\n <unit_amount_in_cents type=\"integer\">15000</unit_amount_in_cents>\n <external_sku>pre-123</external_sku>\n <add_on_type>fixed</add_on_type>\n <usage_percentage nil=\"true\"></usage_percentage>\n <measured_unit_id nil=\"true\"></measured_unit_id>\n </subscription_add_on>\n <subscription_add_on>\n <add_on_code>email_blasts</add_on_code>\n <name>Email Blasts</name>\n <quantity type=\"integer\">1</quantity>\n <external_sku>email-123</external_sku>\n <unit_amount_in_cents type=\"integer\">50</unit_amount_in_cents>\n <add_on_type>usage</add_on_type>\n <usage_percentage nil=\"true\"></usage_percentage>\n <measured_unit_id type=\"integer\">394681687402874853</measured_unit_id>\n </subscription_add_on>\n <subscription_add_on>\n <add_on_code>donations</add_on_code>\n <name>Donations</name>\n <quantity type=\"integer\">1</quantity>\n <unit_amount_in_cents nil=\"true\"></unit_amount_in_cents>\n <add_on_type>usage</add_on_type>\n <usage_percentage>0.6</usage_percentage>\n <measured_unit_id type=\"integer\">394681920153192422</measured_unit_id>\n </subscription_add_on>\n </subscription_add_ons>\n <activated_at type=\"datetime\">2009-11-22T13:10:38Z</activated_at>\n <canceled_at type=\"datetime\"></canceled_at>\n <expires_at type=\"datetime\"></expires_at>\n <current_period_started_at type=\"datetime\">2009-11-22T13:10:38Z</current_period_started_at>\n <current_period_ends_at type=\"datetime\">2009-11-29T13:10:38Z</current_period_ends_at>\n <trial_started_at type=\"datetime\">2009-11-22T13:10:38Z</trial_started_at>\n <trial_ends_at type=\"datetime\">2009-11-29T13:10:38Z</trial_ends_at>\n <collection_method>automatic</collection_method>\n </subscription>\n</new_subscription_notification>");
Request request = new Request.Builder()
.url("http://a346f04e.ngrok.io")
.method("POST", body)
.addHeader("Content-Type", "application/xml")
.build();
Response response = client.newCall(request).execute();
最佳答案
对于正在寻找类似问题答案的人。 删除消耗对我有用。
@RequestMapping(value = "events", method = RequestMethod.POST)
public ModelAndView processEvents(@RequestBody String recurlyEvent, HttpServletRequest request {
Response response = recurlyManager.processEvents(recurlyEvent);
return prepareModelAndView(response);
}
关于java - 使用XML数据时出错内容类型 'application/xml;charset=utf-8' spring不支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61381583/