我是Spring框架的新手,并尝试创建一个api供其他人使用。
这是我的控制器类
POST方法
// -------------------Create a Report-------------------------------------------
@RequestMapping(value = "/report/", method = RequestMethod.POST)
public ResponseEntity<?> createReport(@RequestBody Report report, UriComponentsBuilder ucBuilder) {
logger.info("Creating Report : {}", report);
if (reportRepository.isReportExist(report)) {
logger.error("Unable to create. A report with name {} already exist", report.getCrisisID());
return new ResponseEntity(new CustomErrorType("Unable to create. A Report with crisisID " +
report.getCrisisID() + " already exist."),HttpStatus.CONFLICT);
}
reportRepository.saveReport(report);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(ucBuilder.path("/api/report/{crisisID}").buildAndExpand(report.getCrisisID()).toUri());
return new ResponseEntity<String>(headers, HttpStatus.CREATED);
}
获取方法
// -------------------Retrieve All Reports---------------------------------------------
@RequestMapping(value = "/report/", method = RequestMethod.GET)
public ResponseEntity<List<Report>> listAllReports() {
List<Report> reports = reportRepository.findAllReports();
if (reports.isEmpty()) {
return new ResponseEntity(HttpStatus.NO_CONTENT);
// You many decide to return HttpStatus.NOT_FOUND
}
return new ResponseEntity<List<Report>>(reports, HttpStatus.OK);
}
以下是其他人创建报告对象并将其发送到我的api的示例代码。
// POST
private static void createReport() {
System.out.println("Testing create Report API----------");
RestTemplate restTemplate = new RestTemplate();
Report report = new Report(20, "General", 4, "AA1",10000,"crisis details", "1 hour", "COA1", "COA1");
URI uri = restTemplate.postForLocation(REST_SERVICE_URI + "/report/", report,
Report.class);System.out.println("Location : "+uri.toASCIIString());
}
我想知道以下是否可行或是否有办法解决这种情况:
更新
RestApiController.java
@RestController
@RequestMapping("/api")
public class RestApiController {
public static final Logger logger = LoggerFactory.getLogger(RestApiController.class);
@Autowired
ReportRepository reportRepository; //Service which will do all data retrieval/manipulation work
// -------------------Create a Report-------------------------------------------
@RequestMapping(value = "/report/", method = RequestMethod.POST)
@SendTo("/channel/publicreport")
public ResponseEntity<?> createReport(@RequestBody Report report, UriComponentsBuilder ucBuilder) {
logger.info("Creating Report : {}", report);
if (reportRepository.isReportExist(report)) {
logger.error("Unable to create. A report with name {} already exist", report.getCrisisID());
return new ResponseEntity(new CustomErrorType("Unable to create. A Report with crisisID " +
report.getCrisisID() + " already exist."),HttpStatus.CONFLICT);
}
reportRepository.saveReport(report);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(ucBuilder.path("/api/report/{crisisID}").buildAndExpand(report.getCrisisID()).toUri());
return new ResponseEntity<String>(headers, HttpStatus.CREATED);
}
}
WebSocketConfig.java
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/chatservice");
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app");
registry.enableSimpleBroker("/channel");
}
}
main.js
'use strict';
var stompClient = null;
var username = null;
function connectReport(event) {
username = "myname";
if(username) {
//var socket = new SockJS('/ws');
//stompClient = Stomp.over(socket);
stompClient = Stomp.client('ws://localhost:8080/chatservice');
stompClient.connect({}, onConnectedReport, onError);
}
event.preventDefault();
}
function onConnectedReport() {
// Subscribe to the Public Channel
stompClient.subscribe('/channel/publicreport',onReportMessageReceived);
}
function onError(error) {
connectingElement.textContent = 'Could not connect to WebSocket server. Please refresh this page to try again!';
connectingElement.style.color = 'red';
}
function onReportMessageReceived(payload) {
//Code for pop up window
}
window.addEventListener("load", connectReport, true)
答案 0 :(得分:0)
一般来说,这样的人被委托给客户。即客户端轮询服务器以获取它感兴趣的更改,并且一旦检测到客户端做出相应的反应。最古老的方式(现在已经过时)是在HTML中添加标签,强制整个页面以特定频率刷新,这意味着页面向服务器发送请求并接收要显示的更新页面。在我们这个时代,有各种框架对页面进行部分更新。其中一个是Ajax,然后是DHTML,依此类推。我不是客户端progrqammer。但就概念而言,此类任务通常会提供给客户