我有一个关于Spring MVC的问题,我关于spring mvc的问题是如何连接两个web应用程序,例如在我用我的名字写的第一个web应用程序中,我必须发送该信息并在其他Web应用程序中显示。
您可以在Spring MVC中轻松完成,我将向您解释一个简单的方法。
这里有一些例子,你可以试试自己的
在控制器中获取JSON值
//You can use @RestController or @ResponseBody to send a response in JSON format
@PostMapping(value = "/test", consumes = MediaType.APPLICATION_JSON_VALUE)
public MOResponse receiveNotification(@RequestBody MO mo) {
studentService.getStudent(mo);
MOResponse moResponse = new MOResponse("S1000", "Success");
return moResponse;
}
在此处将POST请求发送到另一个api,此请求以String格式发送,但您可以更改为JSON格式
public void sendMT() {
RestTemplate restTemplate = new RestTemplate();
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
final String uri = "http://localhost:7000/sms/send";
try {
String json = ow.writeValueAsString(mt);
RequestEntity<String> requestEntity = RequestEntity.post(new URL(uri).toURI()).contentType(MediaType.APPLICATION_JSON).body(json);
ResponseEntity<String> output = restTemplate.exchange(requestEntity, String.class);
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
}
在上面的例子中,我已经使用过
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
记住这个例子只是为了得到一个想法。你可以创造自己的,祝你好运
mi首先建议采取课程qazxsw poi
实际上春季开机很容易学弹簧,请查看Spring开机,这是解决您的问题的方法。