java的bean类,为什么用包装类型定义类型,比如Integer 而不是int
int有默认值
springboot,使用get带数组参数的时候,该怎么写。
浏览器访问:http://localhost:8080/selectByBatchId?emps=1&&emps=2
controller层代码:
@GetMapping("/selectByBatchId")
public List selectByBatchId( @RequestParam(“emps”) String [] emps){
return employeeService.selectByBatchId(emps);
}
springboot,使用get带map参数的时候,该怎么写。
浏览器访问:http://localhost:8080/selectByBatchIdMap?last_name=wang&&age=21
controller层代码:
@GetMapping("/selectByBatchIdMap")
public List selectByBatchId(@RequestParam Map<String, Object> emp){
return employeeService.selectByBatchId(emp);
}
springcloud的Eureka,集群配置,切记,spring.application.name中不能出现下划线,否则找不到报500.
springcloud的Eureka,与OpenFeign的配置:
yml配置文件 内联代码片
。
server:
port: 80
eureka:
client:
register-with-eureka: true #这里一定要注意,我看教学视频里这里是false,但是false根本没法用到负载均衡功能,
#所以只要访问controller层servelet就要报错,或者主启动类加上@RibbonClient注解,但是那样一来就有点呆,20201119踩坑。
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
fetch-registry: true
spring:
application:
name: rd-app-userconsumer-service
Controller 内联代码片
。
@RestController
public class WxUserFeignController {
@Resource
private UserFeignService userFeignService;
@GetMapping("/consumer/getWxUserById/{id}")
public CommonResult<WxUserEntity> getWxUserById(@PathVariable Long id) {
return userFeignService.getWxUserById(id);
}
}
Feign接口 内联代码片
。
// An highlighted block
@Component
@FeignClient(value = "RD-APP-USERPAYMENT-SERVICE")
public interface UserFeignService {
@GetMapping("/getWxUserById/{id}")
public CommonResult<WxUserEntity> getWxUserById(@PathVariable("id")Long id);
}
主启动类 `内联代码片`。
```javascript
// An highlighted block
@EnableFeignClients
//@EnableEurekaClient从Spring Cloud Edgware开始,@EnableEurekaClient 或 @EnableDiscoveryClient 是可省略的。只需加上相关依赖,并进行相应配置,即可将微服务注册到服务发现组件上。
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
//@RibbonClient(name="RD-APP-USERPAYMENT-SERVICE",configuration = MySelfRule.class)//这里是定义了自己的负载均衡算法后才加上的注解
public class FeignUserMain80 {
public static void main(String[] args) {
SpringApplication.run(FeignUserMain80.class,args);
}
}
上面是消费端的配置,下面是服务端的Controller
Controller `内联代码片`。
```javascript
// An highlighted block
// when I wrote this,only God and I understood what I was doing
//Now,God only knows
@RestController
public class WxUserController {
@Autowired
private WxUserService wxUserService;
@Resource
private DiscoveryClient discoveryClient;
@Value("${server.port}")
private String serverPort;
@GetMapping("/getWxUserById/{id}")
public CommonResult<WxUserEntity> getWxUserById(@PathVariable Long id){
System.out.println(id);
System.out.println(wxUserService.getWxUserById(id));
WxUserEntity wxUser = wxUserService.getWxUserById(id);
System.out.println("----------------8001-----------------");
if(wxUser != null){
return new CommonResult<>(200,"查询成功,serverPort:"+serverPort,wxUser);
}
else {
return new CommonResult<>(400,"没有对应的记录,查询id"+id,null);
}
}
以上,踩坑的一点就是,消费端得注册进入Eureka,不然访问服务的时候回报错,如果主启动类加上@Ribbonclient(xxxxxx)注解的配置也可以,但是那样负载均衡就是Ribbon在起作用了,所以仍然还是需要Feign注册进入Eureka。
字符串转List集合:
List<Long> ibIdList = JsonUtils.readValue(lpCiEpUser.getIbIds(), new TypeReference<List<Long>>() {});