title: Accept Strings and XML data with RestController
body: <p>I want to create REST Server which accepts XML requests and plain text into different controllers. I tried to implement this:</p>
  
  <pre><code>@SpringBootApplication
  public class Application extends SpringBootServletInitializer implements WebMvcConfigurer {
  
      @Override
      protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
          return application.sources(Application.class);
      }
      ..............
  
      private BasicAuthenticationInterceptor basicAuthenticationInterceptor;
  
      @Override
      public void extendMessageConverters(List&lt;HttpMessageConverter&lt;?&gt;&gt; converters) {
          converters.removeIf(converter -&gt; converter instanceof MappingJackson2XmlHttpMessageConverter);
          converters.removeIf(converter -&gt; converter instanceof MappingJackson2HttpMessageConverter);
          converters.add(new MappingJackson2XmlHttpMessageConverter(
                  ((XmlMapper) createObjectMapper(Jackson2ObjectMapperBuilder.xml()))
                          .enable(ToXmlGenerator.Feature.WRITE_XML_DECLARATION)));
          converters.add(new MappingJackson2HttpMessageConverter(createObjectMapper(Jackson2ObjectMapperBuilder.json())));
      }
  
      private ObjectMapper createObjectMapper(Jackson2ObjectMapperBuilder builder) {
          builder.indentOutput(true);
          builder.modules(new JaxbAnnotationModule());
          builder.serializationInclusion(JsonInclude.Include.NON_NULL);
          builder.defaultUseWrapper(false);
          return builder.build();
      }
  
      @Autowired
      public void setBasicAuthenticationInterceptor(BasicAuthenticationInterceptor basicAuthenticationInterceptor) {
          this.basicAuthenticationInterceptor = basicAuthenticationInterceptor;
      }
  
      @Override
      public void addInterceptors(InterceptorRegistry registry) {
          registry.addInterceptor(basicAuthenticationInterceptor);
      }
  }
  </code></pre>
  
  <p>Check for XML proper formatting:</p>
  
  <pre><code>@ControllerAdvice
  public class RestExceptionHandler extends ResponseEntityExceptionHandler {
  
      @Override
      protected ResponseEntity&lt;Object&gt; handleHttpMessageNotReadable(HttpMessageNotReadableException ex,
                                                                    HttpHeaders headers, HttpStatus status, WebRequest request) {
          PaymentTransaction response;
          if (ex.getMessage().contains("Required request body")) {
              response = new PaymentTransaction(PaymentTransaction.Response.failed_response, 350,
                      "Invalid XML message: No XML data received", "XML request parsing failed!");
          } else {
              response = new PaymentTransaction(PaymentTransaction.Response.failed_response, 351,
                      "Invalid XML message format", null);
          }
          return ResponseEntity.badRequest().body(response);
      }
  }
  </code></pre>
  
  <p>Controller Class:</p>
  
  <pre><code>@RestController()
  public class HomeController {
  
      @Autowired
      public HomeController(Map&lt;String, MessageProcessor&gt; processors, Map&lt;String, ReconcileProcessor&gt; reconcileProcessors,
              @Qualifier("defaultProcessor") MessageProcessor defaultProcessor,
              AuthenticationService authenticationService, ClientRepository repository,
              @Value("${request.limit}") int requestLimit) {
          // Here I receive XML 
      }
  
      @GetMapping(value = "/v1/*")
      public String message() {
          return "REST server";
      }
  
      @PostMapping(value = "/v1/{token}", consumes = { MediaType.APPLICATION_XML_VALUE,
              MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_XML_VALUE,
                      MediaType.APPLICATION_JSON_VALUE })
      public PaymentResponse handleMessage(@PathVariable("token") String token,
              @RequestBody PaymentTransaction transaction, HttpServletRequest request) throws Exception {
          // Here I receive XML 
      }
  
      @PostMapping(value = "/v1/notification")
      public ResponseEntity&lt;String&gt; handleNotifications(@RequestBody Map&lt;String, String&gt; keyValuePairs) {
           // Here I receive key and value in request body
      }
  
      @PostMapping(value = "/v1/summary/by_date/{token}", consumes = { MediaType.APPLICATION_XML_VALUE,
              MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_XML_VALUE,
                      MediaType.APPLICATION_JSON_VALUE })
      public PaymentResponses handleReconcile(@PathVariable("token") String token, @RequestBody Reconcile reconcile,
              HttpServletRequest request) throws Exception {
           // Here I receive XML 
      }
  
      @ResponseStatus(value = HttpStatus.UNAUTHORIZED)
      public static class UnauthorizedException extends RuntimeException {
          UnauthorizedException(String message) {
              super(message);
          }
      }
  }
  </code></pre>
  
  <p>As you can see in some methods I receive XML and in other I receive String in form of <code>key=value&amp;.....</code></p>
  
  <p>How I configure Spring to accept both types?
  Also should I split the Rest controller into different files?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Sample XML request:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;payment_transaction&gt;
    &lt;transaction_type&gt;authorize&lt;/transaction_type&gt;
    &lt;transaction_id&gt;2aeke4geaclv7ml80&lt;/transaction_id&gt;
    &lt;amount&gt;1000&lt;/amount&gt;
    &lt;currency&gt;USD&lt;/currency&gt;
    &lt;card_number&gt;22&lt;/card_number&gt;
    &lt;shipping_address&gt;
      &lt;first_name&gt;Name&lt;/first_name&gt;    
    &lt;/shipping_address&gt;
  &lt;/payment_transaction&gt;
  </code></pre>
  
  <p>Sample XML response:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;payment_response&gt;
      &lt;transaction_type&gt;authorize&lt;/transaction_type&gt;
      &lt;status&gt;approved&lt;/status&gt;
      &lt;unique_id&gt;5f7edd36689f03324f3ef531beacfaae&lt;/unique_id&gt;
      &lt;transaction_id&gt;asdsdlddea4sdaasdsdsa4dadasda&lt;/transaction_id&gt;
      &lt;code&gt;500&lt;/code&gt;
      &lt;amount&gt;101&lt;/amount&gt;
      &lt;currency&gt;EUR&lt;/currency&gt;
  &lt;/payment_response&gt;
  </code></pre>
  
  <p>Sample Notification request:</p>
  
  <pre><code>uniqueid=23434&amp;type=sale&amp;status=33
  </code></pre>
  
  <p>Sample Notification response: It should return only HTTP status OK.</p>
  
  <p>I use:</p>
  
  <pre><code>    &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.0.4.RELEASE&lt;/version&gt;
          &lt;relativePath /&gt;
      &lt;/parent&gt;
  </code></pre>
  
  <p>Java version: "10.0.2" 2018-07-17</p>
  
  <p>About the XML generation I use:</p>
  
  <pre><code>@XmlRootElement(name = "payment_transaction")
  public class PaymentTransaction {
  
      public enum Response {
          failed_response, successful_response
      }
  
      @XmlElement(name = "transaction_type")
      public String transactionType;
      @XmlElement(name = "transaction_id")
      public String transactionId;
      @XmlElement(name = "usage")
  </code></pre>
  
  <p>POM Configuration: <a href="https://pastebin.com/zXqYhDH3" rel="nofollow noreferrer">https://pastebin.com/zXqYhDH3</a></p>