ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • HTTP 메시지 컨버터 : XML
    백기선(인프런 강의)/스프링 웹 MVC 2020. 9. 19. 15:27
    반응형

    OXM(Object-XML Mapper) 라이브러리 중에 스프링이 지원하는 의존성 추가

    • JacksonXML
    • JAXB

     

    스프링 부트를 사용하는 경우

    • 기본으로 XML 의존성 추가해주지 않기 때문에 아래와같이 의존성을 추가해 준다.
    <dependency>
      <groupId>javax.xml.bind</groupId>
      <artifactId>jaxb-api</artifactId>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jaxb</groupId>
      <artifactId>jaxb-runtime</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-oxm</artifactId>
      <version>${spring-framework.version}</version>
    </dependency>
    

     

     

    예제

    package kr.co.study;
    
    import static org.springframework.test.web.client.match.MockRestRequestMatchers.jsonPath;
    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
    import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
    
    import java.io.StringWriter;
    
    import javax.xml.transform.Result;
    import javax.xml.transform.stream.StreamResult;
    
    import org.junit.jupiter.api.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.http.MediaType;
    import org.springframework.oxm.Marshaller;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.test.web.servlet.MockMvc;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @AutoConfigureMockMvc
    public class SampleControllerTest {
    
    	@Autowired
    	MockMvc mockMvc;
    	
    	@Autowired
    	ObjectMapper objectMapper;
    
    	@Autowired
    	Marshaller marshaller;
    	@Test
    	public void xmlMessage() throws Exception{
    		PersonJson person = new PersonJson(30, "xml진석스");
    		StringWriter stringWriter = new StringWriter();
    		Result result = new StreamResult(stringWriter);
    		marshaller.marshal(person, result);
    		String xmlString = stringWriter.toString();
    		
    		this.mockMvc.perform(get("/xmlMessage")
    				.contentType(MediaType.APPLICATION_XML)
    				.accept(MediaType.APPLICATION_XML)
    				.content(xmlString))
    			.andDo(print())
    			.andExpect(status().isOk());
    	}
    
    }
    

     

    package kr.co.study;
    
    import java.util.concurrent.TimeUnit;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.http.CacheControl;
    import org.springframework.oxm.jaxb.Jaxb2Marshaller;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    public class WebConfig implements WebMvcConfigurer {
    
    	@Bean
    	public Jaxb2Marshaller jax2Marshaller() {
    		Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
    		jaxb2Marshaller.setPackagesToScan(PersonXml.class.getPackage().getName());
    		return jaxb2Marshaller;
    	}
    
    }
    

     

    package kr.co.study;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class SampleController {
    		
    	@GetMapping("/xmlMessage")
    	public PersonXml xmlMessage() {
    		PersonXml personXml = new PersonXml(30, "xml진석스");
    		return personXml;
    	}
    }
    

     

    package kr.co.study;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class PersonXml {
    	private int age;
    	private String name;
    
    	public PersonXml(int age, String name) {
    		super();
    		this.age = age;
    		this.name = name;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    }
    
    반응형
Designed by Tistory.