我需要为返回 JAXB 对象的基于 XML 的 Web 服务创建 Web 服务前端。我的前端需要返回一个 RESTful 格式。有没有办法将其转换为 JSON?我的课程路径中有jackson-mapper-asl和jackson-core-asl。
作为一个简单的测试,我返回一个 Book 的 bean,它以 JSON 格式输出,但 JAXB 对象(交付计划)保持 XML 格式。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class DSWLIClient {
@Autowired
private DeliveryScheduleWS wliService;
private BookService bs;
@RequestMapping(value = "/DSDetails/{ds-number}", method = RequestMethod.GET)
public @ResponseBody
DeliverySchedule getDSDetails(@PathVariable("ds-number") String dsNumber) {
DeliveryScheduleResponse dsDetails = wliService.getDeliveryScheduleWSSoap().getDSDetails(dsNumber);
DeliverySchedule deliverySchedule = dsDetails.getDeliverySchedule();
return deliverySchedule;
}
@RequestMapping(value = "/BookDetails/{isbn-number}", method = RequestMethod.GET)
public @ResponseBody
Book getBookDetails(@PathVariable("isbn-number") String isbnNumber) {
bs = new BookService();
Book b = bs.getBookByIsbn(isbnNumber);
System.out.println(b.getAuthor());
return b;
}
}