我写了一个小型项目。返回域信息。域信息和主机信息+状态信息。我在终端中使用cURL util。所有域信息都存储在我的数据库MySQL中 因此他通过这样的终端将其退还给我
C:\Users\Sky>curl -H "Content-Type: application/json" -d "{\"domain\":\"amazon.kz\"}" localhost:8080/api/whois
Domain Name............: amazon.kz
Organization Using Domain Name
Name...................: Amazon.com, Inc.
Organization Name......: Amazon.com, Inc.
Street Address.........: Legal Dept. P.O. Box 81226
City...................: Seattle WA
State..................:
Postal Code............: 98108
Country................: US
Administrative Contact/Agent
Nick Handle............: HOSTERKZ-76523
Phone Number...........: +1.2062664064
Fax....................: +1.2062667010
Email Address..........: admin@internationaladmin.com
Nameserver in listed order
Server.................: 3783652
Ip address.............: 185.98.6.6
Server.................: 3783652
Ip address.............: 104.131.219.250
Server.................: 3783652
Ip address.............: 92.38.225.176
Domain created: 1998-01-02 12:31:33
Last modified: 2013-03-16 10:52:31
Domain status: KAZ
Registrar created: HOSTER.KZ
Current Registrar: clientDeleteProhibited
Domain created: 1998-01-02 12:31:33
Last modified: 2013-03-16 10:52:31
Domain status: KAZ
Registrar created: HOSTER.KZ
Current Registrar: clientTransferProhibited
但是我需要以JSON格式返回它,您可以提供帮助 这是我的Spring Boot项目中的代码。
@RestController
@RequestMapping("api/whois")
public class DomainWhoIsController {
@Autowired
DomainRepository domainRepository;
@Autowired
ContactsRepository contactsRepository;
@Autowired
HostRepository hostRepository;
@Autowired
StatusRepository statusRepository;
@PostMapping
public String whoIs(@RequestBody Request request) {
System.out.println(request);
DomainInfo domainInfo = domainRepository.findDomainInfo(request.getDomain());
ContactInfo contactInfo = contactsRepository.findContactInfo(request.getDomain());
List<HostInfo> hostInfo = hostRepository.findHostInfo(request.getDomain());
List<StatusInfo> statusInfo = statusRepository.findStatusInfo(request.getDomain());
return report(domainInfo, contactInfo, hostInfo, statusInfo);
}
private String report(DomainInfo domainInfo, ContactInfo contactInfo, List<HostInfo> hostInfo, List<StatusInfo> statusInfo) {
StringBuffer sb = new StringBuffer();
sb
.append("Domain Name............: ").append(domainInfo.getDomainName()).append(System.lineSeparator())
.append(System.lineSeparator())
.append("Organization Using Domain Name").append(System.lineSeparator())
.append("Name...................: ").append(domainInfo.getName()).append(System.lineSeparator())
.append("Organization Name......: ").append(domainInfo.getOrganizationName()).append(System.lineSeparator())
.append("Street Address.........: ").append(domainInfo.getStreetAddress()).append(System.lineSeparator())
.append("City...................: ").append(domainInfo.getCity()).append(System.lineSeparator())
.append("State..................: ").append(domainInfo.getState()).append(System.lineSeparator())
.append("Postal Code............: ").append(domainInfo.getPostalCode()).append(System.lineSeparator())
.append("Country................: ").append(domainInfo.getCountry()).append(System.lineSeparator())
.append(System.lineSeparator())
.append("Administrative Contact/Agent").append(System.lineSeparator())
.append("Nick Handle............: ").append(contactInfo.getHandle()).append(System.lineSeparator())
.append("Phone Number...........: ").append(contactInfo.getVoice()).append(System.lineSeparator())
.append("Fax....................: ").append(contactInfo.getFax()).append(System.lineSeparator())
.append("Email Address..........: ").append(contactInfo.getEmail()).append(System.lineSeparator())
.append(System.lineSeparator())
.append("Nameserver in listed order").append(System.lineSeparator())
.append(System.lineSeparator());
hostInfo.forEach(hInfo -> sb
.append("Server.................: ").append(hInfo.getDomain()).append(System.lineSeparator())
.append("Ip address.............: ").append(hInfo.getIpaddr()).append(System.lineSeparator())
);
sb.append(System.lineSeparator());
statusInfo.forEach(sInfo -> sb
.append("Domain created: ").append(sInfo.getCreated()).append(System.lineSeparator())
.append("Last modified: ").append(sInfo.getUpdated()).append(System.lineSeparator())
.append("Domain status: ").append(sInfo.getStatus()).append(System.lineSeparator())
.append("Registrar created: ").append(sInfo.getRegistarcr()).append(System.lineSeparator())
.append("Current Registrar: ").append(sInfo.getRegistrar()).append(System.lineSeparator())
);
return sb.toString();
}
}
答案 0 :(得分:2)
如果要返回DomainInfo,只需将returnType更改为DomainInfo。
@PostMapping
public DomainInfo whoIs(@RequestBody Request request) {
// ...
DomainInfo domainInfo = domainRepository.findDomainInfo(request.getDomain());
// ...
return domainInfo;
}
或者您希望将所有对象放在一起,只需创建包含所有对象的新类,然后将该类用作returnType即可:
WhoIs.class
public class WhoIs {
// All Args Constructor
private DomainInfo domainInfo;
private ContactInfo contactInfo;
private List<HostInfo> hostInfo;
private List<StatusInfo> statusInfo;
// getter & setter
}
DomainWhoIsController.class
@PostMapping
public WhoIs whoIs(@RequestBody Request request) {
System.out.println(request);
DomainInfo domainInfo = domainRepository.findDomainInfo(request.getDomain());
ContactInfo contactInfo = contactsRepository.findContactInfo(request.getDomain());
List<HostInfo> hostInfo = hostRepository.findHostInfo(request.getDomain());
List<StatusInfo> statusInfo = statusRepository.findStatusInfo(request.getDomain());
return new WhoIs(domainInfo, contactInfo, hostInfo, statusInfo);
}