package com.example.demo;
public class Echo {
private final long id;
private final String content;
public Echo(long id, String content) {
this.id = id;
this.content = content;
}
public long getId() {
return this.id;
}
public String getContent() {
return this.content;
}
}
package com.example.demo;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ModelAttribute;
@RestController
@RequestMapping("/echo")
public class EchoController {
private static final String echoTemplate1 = "received %s!";
private static final String echoTemplate2 = "%s speak to %s \'%s\'";
private final AtomicLong counter = new AtomicLong();
@RequestMapping(value="/getter/pattern1", method=RequestMethod.GET)
public Echo getterPattern1(String content) {
return new Echo(counter.incrementAndGet(), String.format(echoTemplate1, content));
}
@RequestMapping(value="/getter/pattern2", method=RequestMethod.GET)
public Echo getterPattern2(@RequestParam(value="content", required=false) String alias) {
return new Echo(counter.incrementAndGet(), String.format(echoTemplate1, alias));
}
}
{"id":6,"content":"received hello!"}
@RequestMapping(value="/getter/pattern3/{content}", method=RequestMethod.GET)
public Echo getterPattern3(@PathVariable String content) {
return new Echo(counter.incrementAndGet(), String.format(echoTemplate1, content));
}
{"id":8,"content":"received 123456!"}
package com.example.demo;
public class Message {
private String from;
private String to;
private String content;
public Message() {}
public String getFrom() {
return this.from;
}
public String getTo() {
return this.to;
}
public String getContent() {
return this.content;
}
public void setFrom(String value) {
this.from = value;
}
public void setTo(String value) {
this.to = value;
}
public void setContent(String value) {
this.content = value;
}
}
@RequestMapping(value="/setter/message1", method=RequestMethod.POST)
public Echo setterMessage1(@RequestBody Message message) {
return new Echo(counter.incrementAndGet(), String.format(echoTemplate2, message.getFrom(), message.getTo(), message.getContent()));
}
curl -i -H "Content-Type:application/json" -d "{\"from\":\"Tom\",\"to\":\"Sandy\",\"content\":\"hello buddy\"}" http://localhost:8080/echo/setter/message1
{"id":9,"content":"Tom speak to Sandy 'hello buddy'"}
@RequestMapping(value="/setter/message2", method=RequestMethod.POST)
public Echo setterMessage2(@ModelAttribute Message message) {
return new Echo(counter.incrementAndGet(), String.format(echoTemplate2, message.getFrom(), message.getTo(), message.getContent()));
}
curl -i -H "Content-Type:application/x-www-form-urlencoded" -d "from=sandy&to=aissen&content=go to" http://localhost:8080/echo/setter/message2
{"id":11,"content":"sandy speak to aissen 'go to'"}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有