-1

What are the maven dependencies need to be added to get the XML output without configuring content negotiation view resolver and managers. By using the default Message Converters based on jars on classpath (output based on accept headers). I am able to get the JSON output by having jackson-databind dependency on the classpath. For XML I am using

  <dependency>
   <groupId>javax.xml.bind</groupId>
   <artifactId>jaxb-api</artifactId>
   <version>2.2.7</version>
  </dependency>

  <dependency>
   <groupId>com.sun.xml.bind</groupId>
   <artifactId>jaxb-impl</artifactId>
   <version>2.2.7</version>
  </dependency>

 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-oxm</artifactId>
  <version>${org.springframework.version}</version>
 </dependency>

dependencies - I am unable to get the XML output. DO I need configure any Marshallers like Jaxb2Marsahllar as a bean in the configuration file. Can Any post the maven dependencies for JAXB2.

My Entity class:

    package com.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Table(name = "Employee")
@XmlRootElement
public class Employee {

    public Employee() {
    }

    public Employee(Integer empno, String name, String dept, Double salary) {
        this.empno = empno;
        this.name = name;
        this.dept = dept;
        this.salary = salary;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer empno;

    @Size(min = 1, max = 30)
    @NotEmpty
    private String name;

    @NotEmpty
    @Size(min = 1, max = 30)
    private String dept;

    /*
     * @NotEmpty - cannot be set to double - supports String Collection Map
     * arrays
     */

    private Double salary;

    @XmlAttribute
    public Integer getEmpno() {
        return empno;
    }

    public void setEmpno(Integer empno) {
        this.empno = empno;
    }

    @XmlElement
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlElement
    public String getDept() {
        return dept;
    }

    public void setDept(String dept) {
        this.dept = dept;
    }

    @XmlElement
    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee [empno=" + empno + ", name=" + name + ", dept=" + dept
                + ", salary=" + salary + "]";
    }
}

My Controller Class:

@Controller

public class EmployeeController {

@Autowired
EmployeeRepository employeeRepository;


@RequestMapping(value = "/employees", method=RequestMethod.GET,
        produces= {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody List<Employee> findAllXml(){
    return employeeRepository.findAll();
}

}

Please Can any one say Whether the dependencies are enough ? What needs to be added..

Dinesh Dontha
  • 537
  • 5
  • 17

2 Answers2

0

put @XMLElement on set methods.

public Integer getEmpno() {
    return empno;
}

@XmlAttribute
public void setEmpno(Integer empno) {
    this.empno = empno;
}


public String getName() {
    return name;
}

@XmlElement
public void setName(String name) {
    this.name = name;
}

or you can use Spring Marshalling View of spring-oxm.jar

<bean id="xmlViewer" 
    class="org.springframework.web.servlet.view.xml.MarshallingView">
    <constructor-arg>
      <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="classesToBeBound">
            <list>
                <value>com.model.Employee</value>
            </list>
        </property>
      </bean>
    </constructor-arg>
</bean>

Update:1 Also findAll is returning list that list needs to be accomodated in a parent tag like

 <Employees>
  <Employee />
  <Employee />
</Employees>

so you need to define a class that has an @XMLElement entity as List<Employees> create object of it put the data in it and return that object.

Neenad
  • 861
  • 5
  • 19
  • Hi, I have used @XmlElement, but I am getting 406 error Media Type not supported exception. Here is the sample code : https://drive.google.com/file/d/0B_gwHb72nZGUVHZ0X29OUkE4S1E/view?usp=sharing – Dinesh Dontha Oct 20 '15 at 14:03
0

I found the answer to 406 exception

Problem was needed an extra configuration for Message Converters for XML output.

For XML Output, we need to Added a Message Converter to the list of Message converters of RequestMappingHandlerAdapter

But for JSON we dont need to do this explictly, based on the jackson-databind dependencies on the classpath, we can able get the JSON output. But for xml , we need to add a message converter (MarshallingHttpMessageConverter) .

Example: Using Java Based Config: configuring RequestMappingHandlerAdapter as a bean and adding required Message Converters...

@Bean
public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
    RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();

    List<HttpMessageConverter<?>> converters = new ArrayList();
    converters.add(new MarshallingHttpMessageConverter(
            new XStreamMarshaller()));
    converters.add(new MappingJackson2HttpMessageConverter());

    adapter.setMessageConverters(converters);
    return adapter;
}

I am using XStream Marshaller, so need to add its dependencies as well

<dependency>
 <groupId>com.thoughtworks.xstream</groupId>
 <artifactId>xstream</artifactId>
 <version>1.4.8</version>
</dependency>

Example Tests:

    @Test
public void testXml() throws Exception {
    this.mvc.perform(get("/employees/xml").accept(APPLICATION_XML))
    .andDo(print())
    .andExpect(content().contentType("application/xml"));       
}


@Test
public void testJson() throws Exception {
    this.mvc.perform(get("/employees/json").accept(APPLICATION_JSON))
    .andDo(print())
    .andExpect(content().contentType("application/json"));      
}

Please post if you know any other way of doing this. useful link: Spring XML 406 error

Community
  • 1
  • 1
Dinesh Dontha
  • 537
  • 5
  • 17