example annotations clearer

pull/876/head
赵禹光 6 years ago
parent 00835121cb
commit 3eb75988e3

@ -19,7 +19,7 @@ package com.alibaba.cloud.examples;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -34,7 +34,7 @@ public class HomeController {
@Autowired @Autowired
private EchoService echoService; private EchoService echoService;
@RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json") @GetMapping(value = "/", produces = "application/json")
public String home() { public String home() {
LOGGER.info("-----------------consumer调用开始-----------------"); LOGGER.info("-----------------consumer调用开始-----------------");
String param = "Nice to meet you."; String param = "Nice to meet you.";

@ -19,6 +19,7 @@ package com.alibaba.cloud.examples;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -35,7 +36,7 @@ public class HomeController {
@Autowired @Autowired
private RestTemplate restTemplate; private RestTemplate restTemplate;
@RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json") @GetMapping(value = "/", produces = "application/json")
public String home() { public String home() {
LOGGER.info("-----------------consumer调用开始-----------------"); LOGGER.info("-----------------consumer调用开始-----------------");
String param = "Nice to meet you."; String param = "Nice to meet you.";

@ -18,10 +18,7 @@ package com.alibaba.cloud.examples;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/** /**
* @author xiaolongzuo * @author xiaolongzuo
@ -31,7 +28,7 @@ public class EchoController {
private static final Logger LOGGER = LoggerFactory.getLogger(EchoController.class); private static final Logger LOGGER = LoggerFactory.getLogger(EchoController.class);
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET, produces = "application/json") @GetMapping(value = "/echo/{str}", produces = "application/json")
public String echo(@PathVariable String str) { public String echo(@PathVariable String str) {
LOGGER.info("-----------收到消费者请求-----------"); LOGGER.info("-----------收到消费者请求-----------");
LOGGER.info("收到消费者传递的参数:" + str); LOGGER.info("收到消费者传递的参数:" + str);

@ -7,10 +7,7 @@ import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import com.alibaba.cloud.examples.ConsumerApplication.EchoService; import com.alibaba.cloud.examples.ConsumerApplication.EchoService;
@ -43,17 +40,17 @@ public class ConsumerApplication {
@FeignClient(name = "service-provider", fallback = EchoServiceFallback.class, configuration = FeignConfiguration.class) @FeignClient(name = "service-provider", fallback = EchoServiceFallback.class, configuration = FeignConfiguration.class)
public interface EchoService { public interface EchoService {
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET) @GetMapping(value = "/echo/{str}")
String echo(@PathVariable("str") String str); String echo(@PathVariable("str") String str);
@RequestMapping(value = "/divide", method = RequestMethod.GET) @GetMapping(value = "/divide")
String divide(@RequestParam("a") Integer a, @RequestParam("b") Integer b); String divide(@RequestParam("a") Integer a, @RequestParam("b") Integer b);
default String divide(Integer a) { default String divide(Integer a) {
return divide(a, 0); return divide(a, 0);
} }
@RequestMapping(value = "/notFound", method = RequestMethod.GET) @GetMapping(value = "/notFound")
String notFound(); String notFound();
} }

@ -2,11 +2,7 @@ package com.alibaba.cloud.examples;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import com.alibaba.cloud.examples.ConsumerApplication.EchoService; import com.alibaba.cloud.examples.ConsumerApplication.EchoService;
@ -44,53 +40,53 @@ public class TestController {
// }); // });
// } // }
@RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET) @GetMapping(value = "/echo-rest/{str}")
public String rest(@PathVariable String str) { public String rest(@PathVariable String str) {
return restTemplate.getForObject("http://service-provider/echo/" + str, return restTemplate.getForObject("http://service-provider/echo/" + str,
String.class); String.class);
} }
@RequestMapping(value = "/index", method = RequestMethod.GET) @GetMapping(value = "/index")
public String index() { public String index() {
return restTemplate1.getForObject("http://service-provider", String.class); return restTemplate1.getForObject("http://service-provider", String.class);
} }
@RequestMapping(value = "/test", method = RequestMethod.GET) @GetMapping(value = "/test")
public String test() { public String test() {
return restTemplate1.getForObject("http://service-provider/test", String.class); return restTemplate1.getForObject("http://service-provider/test", String.class);
} }
@RequestMapping(value = "/sleep", method = RequestMethod.GET) @GetMapping(value = "/sleep")
public String sleep() { public String sleep() {
return restTemplate1.getForObject("http://service-provider/sleep", String.class); return restTemplate1.getForObject("http://service-provider/sleep", String.class);
} }
@RequestMapping(value = "/notFound-feign", method = RequestMethod.GET) @GetMapping(value = "/notFound-feign")
public String notFound() { public String notFound() {
return echoService.notFound(); return echoService.notFound();
} }
@RequestMapping(value = "/divide-feign", method = RequestMethod.GET) @GetMapping(value = "/divide-feign")
public String divide(@RequestParam Integer a, @RequestParam Integer b) { public String divide(@RequestParam Integer a, @RequestParam Integer b) {
return echoService.divide(a, b); return echoService.divide(a, b);
} }
@RequestMapping(value = "/divide-feign2", method = RequestMethod.GET) @GetMapping(value = "/divide-feign2")
public String divide(@RequestParam Integer a) { public String divide(@RequestParam Integer a) {
return echoService.divide(a); return echoService.divide(a);
} }
@RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET) @GetMapping(value = "/echo-feign/{str}")
public String feign(@PathVariable String str) { public String feign(@PathVariable String str) {
return echoService.echo(str); return echoService.echo(str);
} }
@RequestMapping(value = "/services/{service}", method = RequestMethod.GET) @GetMapping(value = "/services/{service}")
public Object client(@PathVariable String service) { public Object client(@PathVariable String service) {
return discoveryClient.getInstances(service); return discoveryClient.getInstances(service);
} }
@RequestMapping(value = "/services", method = RequestMethod.GET) @GetMapping(value = "/services")
public Object services() { public Object services() {
return discoveryClient.getServices(); return discoveryClient.getServices();
} }

@ -5,11 +5,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/** /**
* @author xiaojing * @author xiaojing
@ -25,17 +21,17 @@ public class ProviderApplication {
@RestController @RestController
class EchoController { class EchoController {
@RequestMapping(value = "/", method = RequestMethod.GET) @GetMapping(value = "/")
public ResponseEntity index() { public ResponseEntity index() {
return new ResponseEntity("index error", HttpStatus.INTERNAL_SERVER_ERROR); return new ResponseEntity("index error", HttpStatus.INTERNAL_SERVER_ERROR);
} }
@RequestMapping(value = "/test", method = RequestMethod.GET) @GetMapping(value = "/test")
public ResponseEntity test() { public ResponseEntity test() {
return new ResponseEntity("error", HttpStatus.INTERNAL_SERVER_ERROR); return new ResponseEntity("error", HttpStatus.INTERNAL_SERVER_ERROR);
} }
@RequestMapping(value = "/sleep", method = RequestMethod.GET) @GetMapping(value = "/sleep")
public String sleep() { public String sleep() {
try { try {
Thread.sleep(1000L); Thread.sleep(1000L);
@ -46,12 +42,12 @@ public class ProviderApplication {
return "ok"; return "ok";
} }
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET) @GetMapping(value = "/echo/{string}")
public String echo(@PathVariable String string) { public String echo(@PathVariable String string) {
return "hello Nacos Discovery " + string; return "hello Nacos Discovery " + string;
} }
@RequestMapping(value = "/divide", method = RequestMethod.GET) @GetMapping(value = "/divide")
public String divide(@RequestParam Integer a, @RequestParam Integer b) { public String divide(@RequestParam Integer a, @RequestParam Integer b) {
return String.valueOf(a / b); return String.valueOf(a / b);
} }

@ -17,6 +17,7 @@
package com.alibaba.cloud.examples; package com.alibaba.cloud.examples;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -27,7 +28,7 @@ public class GetConfigController {
@Value("${config}") @Value("${config}")
private String config; private String config;
@RequestMapping(value = "/config", method = RequestMethod.GET) @GetMapping(value = "/config")
public String getConfig() { public String getConfig() {
return config; return config;
} }

@ -35,7 +35,7 @@
@RestController @RestController
class EchoController { class EchoController {
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET) @GetMapping(value = "/echo/{string}")
public String echo(@PathVariable String string) { public String echo(@PathVariable String string) {
return string; return string;
} }
@ -120,11 +120,11 @@ Nacos Discovery Starter 默认集成了 Ribbon ,所以对于使用了 Ribbon
@Autowired @Autowired
private EchoService echoService; private EchoService echoService;
@RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET) @GetMapping(value = "/echo-rest/{str}")
public String rest(@PathVariable String str) { public String rest(@PathVariable String str) {
return restTemplate.getForObject("http://service-provider/echo/" + str, String.class); return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
} }
@RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET) @GetMapping(value = "/echo-feign/{str}")
public String feign(@PathVariable String str) { public String feign(@PathVariable String str) {
return echoService.echo(str); return echoService.echo(str);
} }

@ -34,7 +34,7 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl
@RestController @RestController
class EchoController { class EchoController {
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET) @GetMapping(value = "/echo/{string}")
public String echo(@PathVariable String string) { public String echo(@PathVariable String string) {
return string; return string;
} }
@ -123,11 +123,11 @@ The code of `nacos-discovery-consumer-example` project will be analyzed below, d
@Autowired @Autowired
private EchoService echoService; private EchoService echoService;
@RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET) @GetMapping(value = "/echo-rest/{str}")
public String rest(@PathVariable String str) { public String rest(@PathVariable String str) {
return restTemplate.getForObject("http://service-provider/echo/" + str, String.class); return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
} }
@RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET) @GetMapping(value = "/echo-feign/{str}")
public String feign(@PathVariable String str) { public String feign(@PathVariable String str) {
return echoService.echo(str); return echoService.echo(str);
} }

@ -3,11 +3,7 @@ package com.alibaba.cloud.examples;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/** /**
* @author xiaojing * @author xiaojing
@ -22,12 +18,12 @@ public class ProviderApplication {
@RestController @RestController
class EchoController { class EchoController {
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET) @GetMapping(value = "/echo/{string}")
public String echo(@PathVariable String string) { public String echo(@PathVariable String string) {
return "hello Nacos Discovery " + string; return "hello Nacos Discovery " + string;
} }
@RequestMapping(value = "/divide", method = RequestMethod.GET) @GetMapping(value = "/divide")
public String divide(@RequestParam Integer a, @RequestParam Integer b) { public String divide(@RequestParam Integer a, @RequestParam Integer b) {
return String.valueOf(a / b); return String.valueOf(a / b);
} }

@ -20,6 +20,7 @@ import java.util.Random;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -45,7 +46,7 @@ public class AccountController {
this.random = new Random(); this.random = new Random();
} }
@RequestMapping(value = "/account", method = RequestMethod.POST, produces = "application/json") @PostMapping(value = "/account", produces = "application/json")
public String account(String userId, int money) { public String account(String userId, int money) {
LOGGER.info("Account Service ... xid: " + RootContext.getXID()); LOGGER.info("Account Service ... xid: " + RootContext.getXID());

@ -21,6 +21,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
@ -54,7 +55,7 @@ public class BusinessApplication {
@FeignClient(value = "order", url = "http://127.0.0.1:18083") @FeignClient(value = "order", url = "http://127.0.0.1:18083")
public interface OrderService { public interface OrderService {
@RequestMapping(path = "/order", method = RequestMethod.POST) @PostMapping(path = "/order")
String order(@RequestParam("userId") String userId, String order(@RequestParam("userId") String userId,
@RequestParam("commodityCode") String commodityCode, @RequestParam("commodityCode") String commodityCode,
@RequestParam("orderCount") int orderCount); @RequestParam("orderCount") int orderCount);

@ -24,6 +24,7 @@ import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -60,7 +61,7 @@ public class HomeController {
} }
@GlobalTransactional(timeoutMills = 300000, name = "spring-cloud-demo-tx") @GlobalTransactional(timeoutMills = 300000, name = "spring-cloud-demo-tx")
@RequestMapping(value = "/seata/rest", method = RequestMethod.GET, produces = "application/json") @GetMapping(value = "/seata/rest", produces = "application/json")
public String rest() { public String rest() {
String result = restTemplate.getForObject( String result = restTemplate.getForObject(

@ -33,6 +33,7 @@ import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder; import org.springframework.jdbc.support.KeyHolder;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -62,7 +63,7 @@ public class OrderController {
this.random = new Random(); this.random = new Random();
} }
@RequestMapping(value = "/order", method = RequestMethod.POST, produces = "application/json") @PostMapping(value = "/order", produces = "application/json")
public String order(String userId, String commodityCode, int orderCount) { public String order(String userId, String commodityCode, int orderCount) {
LOGGER.info("Order Service Begin ... xid: " + RootContext.getXID()); LOGGER.info("Order Service Begin ... xid: " + RootContext.getXID());

@ -19,10 +19,7 @@ package com.alibaba.cloud.examples;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import io.seata.core.context.RootContext; import io.seata.core.context.RootContext;
@ -43,7 +40,7 @@ public class StorageController {
this.jdbcTemplate = jdbcTemplate; this.jdbcTemplate = jdbcTemplate;
} }
@RequestMapping(value = "/storage/{commodityCode}/{count}", method = RequestMethod.GET, produces = "application/json") @GetMapping(value = "/storage/{commodityCode}/{count}", produces = "application/json")
public String echo(@PathVariable String commodityCode, @PathVariable int count) { public String echo(@PathVariable String commodityCode, @PathVariable int count) {
LOGGER.info("Storage Service Begin ... xid: " + RootContext.getXID()); LOGGER.info("Storage Service Begin ... xid: " + RootContext.getXID());
int result = jdbcTemplate.update( int result = jdbcTemplate.update(

@ -1,6 +1,7 @@
package com.alibaba.cloud.examples; package com.alibaba.cloud.examples;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -17,24 +18,24 @@ public class TestController {
@Autowired @Autowired
private RestTemplate restTemplate; private RestTemplate restTemplate;
@RequestMapping(value = "/hello", method = RequestMethod.GET) @GetMapping(value = "/hello")
@SentinelResource("resource") @SentinelResource("resource")
public String hello() { public String hello() {
return "Hello"; return "Hello";
} }
@RequestMapping(value = "/aa", method = RequestMethod.GET) @GetMapping(value = "/aa")
@SentinelResource("aa") @SentinelResource("aa")
public String aa(int b, int a) { public String aa(int b, int a) {
return "Hello test"; return "Hello test";
} }
@RequestMapping(value = "/test", method = RequestMethod.GET) @GetMapping(value = "/test")
public String test1() { public String test1() {
return "Hello test"; return "Hello test";
} }
@RequestMapping(value = "/template", method = RequestMethod.GET) @GetMapping(value = "/template")
public String client() { public String client() {
return restTemplate.getForObject("http://www.taobao.com/test", String.class); return restTemplate.getForObject("http://www.taobao.com/test", String.class);
} }

Loading…
Cancel
Save