Spring Boot REST with XML Support

前端 未结 2 1572
难免孤独
难免孤独 2021-01-18 00:39

I made a simple REST webservice with Spring Boot 1.2.5 and it works fine for JSON but I can\'t make this work to return XML.

This is my controller:

         


        
相关标签:
2条回答
  • 2021-01-18 00:48

    To make this work in Spring Boot without using Jersey we need to add this dependency:

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

    The output will be a bit ugly but it works:

    <ArrayList 
        xmlns="">
        <item>
            <id/>
            <description>Swimming</description>
            <duration>55</duration>
            <user/>
        </item>
        <item>
            <id/>
            <description>Cycling</description>
            <duration>120</duration>
            <user/>
        </item>
    </ArrayList>
    

    Here is nice tutorial: http://www.javacodegeeks.com/2015/04/jax-rs-2-x-vs-spring-mvc-returning-an-xml-representation-of-a-list-of-objects.html

    0 讨论(0)
  • 2021-01-18 01:02

    We can achieve this as below :

    Code


            package com.subu;
    
            import java.io.Serializable;
    
            import javax.persistence.Entity;
            import javax.persistence.GeneratedValue;
            import javax.persistence.Id;
            import javax.persistence.Table;
            import javax.xml.bind.annotation.*;
    
            @Entity
            @XmlRootElement(name = "person")
            @Table(name="person")
    
            public class Person implements Serializable{
                private static final long serialVersionUID = 1L;
    
                @Id
                @GeneratedValue
                private Long id;
    
                @XmlAttribute(name = "first-name")
                private String first_name;
    
                public Long getId() {
                    return id;
                }
    
                public void setId(Long id) {
                    this.id = id;
                }
    
                public String getFirst_name() {
                    return first_name;
                }
    
                public void setFirst_name(String first_name) {
                    this.first_name = first_name;
                }
    
                public String getLast_name() {
                    return last_name;
                }
    
                public void setLast_name(String last_name) {
                    this.last_name = last_name;
                }
    
                public String getDate_of_birth() {
                    return date_of_birth;
                }
    
                public void setDate_of_birth(String date_of_birth) {
                    this.date_of_birth = date_of_birth;
                }
    
                @XmlAttribute(name = "last-name")
                private String last_name;
    
                @XmlAttribute(name = "dob")
                private String date_of_birth;
    
    
            }
    

            @RestController
            public class PersonController {
    
                @Autowired
                private PersonRepository personRepository;
    
                @RequestMapping(value = "/persons/{id}", method = RequestMethod.GET,produces={MediaType.APPLICATION_XML_VALUE},headers = "Accept=application/xml")
                public ResponseEntity<?> getPersonDetails(@PathVariable Long id, final HttpServletRequest request)throws Exception {
                    Person personResponse=personRepository.findPersonById(id);
                    return ResponseEntity.ok(personResponse);
                }
    
            }
    

            package com.subu;
    
            import org.springframework.boot.SpringApplication;
            import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
            import org.springframework.boot.autoconfigure.SpringBootApplication;
            import org.springframework.boot.builder.SpringApplicationBuilder;
            import org.springframework.boot.context.web.SpringBootServletInitializer;
            import org.springframework.context.annotation.ComponentScan;
            import org.springframework.context.annotation.Configuration;
            import org.springframework.scheduling.annotation.EnableScheduling;
    
    
            @SpringBootApplication
            @Configuration
            @ComponentScan
            @EnableAutoConfiguration
            @EnableScheduling
            public class Application extends SpringBootServletInitializer{
    
    
    
               public static void main(String[] args) {
                  SpringApplication.run(Application.class, args);
               }
    
               @Override
               protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
                   return application.sources(Application.class);
               }
    
               private static Class<Application> applicationClass = Application.class;
    
            }
    

    0 讨论(0)
提交回复
热议问题