> For the complete documentation index, see [llms.txt](https://blakes-organization.gitbook.io/rainsister/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blakes-organization.gitbook.io/rainsister/springboot-2.x/rest-api/xml.md).

# XML에 대한 요청 및 응답 처리

현재까지 우리는 json , html 관련 요청 및 응답처리만 해왔는데 이번에는 xml 관련처리에 대해서 알아보자

일단 message converter 를 사용해야 되니 인터페이스를 정의해보자.

```java
public interface HttpMessageConverter<T> {

    boolean canRead(Class<?> clazz, @Nullable MediaType mediaType);

    boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType);

    List<MediaType> getSupportedMediaTypes();

    T read(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException;

    void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException;

}
```

이미 다들 알고 있었지만 Http 요청의 content-Type은 다양하다. xml로 전화하려면 해당 converter 를 사용해야 된다. SpringMVC는 기본적으로 Jackson 으로 구현 된 MappingJackson2XmlHttpMessageConverter 객체를 제공한다.&#x20;

Spring (springboot아님) 에서는 아래와 같이 했었다.

```java
@Configuration
public class MessageConverterConfig1 extends WebMvcConfigurerAdapter {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();
        builder.indentOutput(true);
        converters.add(new MappingJackson2XmlHttpMessageConverter(builder.build()));
    }
}
```

그럼 Springboot 에서는 조금 더 쉽게 할수 있지 않을까 ? 맞다 바로  아래와 같이.

pom.xml dependency 추가 하면 자동으로 MappingJackson2XmlHttpMessageConverter 를 사용할수 있게 된다.

```xml
<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
</dependency>
```

xml 전환하려는 객체 생성

```java
@Data
@NoArgsConstructor
@AllArgsConstructor
@JacksonXmlRootElement(localName = "User")
public class User {

    @JacksonXmlProperty(localName = "name")
    private String name;
    @JacksonXmlProperty(localName = "age")
    private Integer age;

}
```

{% hint style="info" %}
@Data、@NoArgsConstructor、@AllArgsConstructor Lombok 전용이다.\@JacksonXmlRootElement、@JacksonXmlProperty 만 참고하면 된다.
{% endhint %}

#### xml 작성

```xml
<User>
    <name>aaaa</name>
    <age>10</age>
</User>
```

#### xml요청을 받는 controller 를 만든다.

```java
@Controller
public class UserController {

    @PostMapping(value = "/user", 
        consumes = MediaType.APPLICATION_XML_VALUE, 
        produces = MediaType.APPLICATION_XML_VALUE)
    @ResponseBody
    public User create(@RequestBody User user) {
        user.setName("didispace.com : " + user.getName());
        user.setAge(user.getAge() + 100);
        return user;
    }

}
```

POSTMAN 으로 xml 요청을 해보자.

<figure><img src="/files/0fju3GCbcLY9HFLAF1Q7" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blakes-organization.gitbook.io/rainsister/springboot-2.x/rest-api/xml.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
