Add below in Transaction class:
import javax.xml.bind.annotation.XmlRootElement;
import org.springframework.stereotype.Component;
@Component
@XmlRootElement
public class Transaction {
----
}
In controller:
@RequestMapping(value = "/transaction/getTransDoneBy",
method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE)
public Transaction getTransDoneBy() {
transaction.setTransId("4567");
transaction.setTrasSource("Amazon");
transaction.setTransactionDoneBy("JBK");
return transaction;
}
URL: http://localhost:8080/transaction/getTransDoneBy
<transaction>
<transId>4567</transId>
<transactionDoneBy>JBK</transactionDoneBy>
<trasSource>Amazon</trasSource>
</transaction>
Likewise there will be many flavors you can see below is complete snap of controller.
package com.javabykiran;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TransactionController {
@Autowired
Transaction transaction;
@RequestMapping("/transaction/get")
public Transaction getTransaction() {
transaction.setTransId("1234");
transaction.setTrasSource("Amazon");
transaction.setTransactionDoneBy("JBK");
return transaction;
}
@RequestMapping("/transaction/getId")
public Transaction getTransaction(
@RequestParam(name = "transId", required = false,
defaultValue = "Unknown") String transId) {
transaction.setTransId(transId);
transaction.setTrasSource("Amazon");
transaction.setTransactionDoneBy("JBK");
return transaction;
}
@RequestMapping("/transaction/getAllTrans")
public List<Transaction> getAllTransaction() {
List<Transaction>listTransaction = new ArrayList<>();
Transaction trans = new Transaction("123", "kiran", "Flipkart");
Transaction trans1 = new Transaction("456", "jbk", "Irctc");
listTransaction.add(trans);
listTransaction.add(trans1);
return listTransaction;
}
@RequestMapping(value = "/transaction/getTransDoneBy",
method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE)
public Transaction getTransDoneBy() {
transaction.setTransId("4567");
transaction.setTrasSource("Amazon");
transaction.setTransactionDoneBy("JBK");
return transaction;
}
}