webservice 项目中遇到的问题
redshift database 连接异常
解决方案
url 修改添加参数如下
jdbc:redshift://hostname:5439/dbname?ssl=true&sslfactory=com.amazon.redshift.ssl.NonValidatingFactory
java 8 lambda 表达式应用
// 拆分 参数并去左右空格
public static List<String> splitStr2List(String params,String delimeter){
return Arrays.asList(params.split(delimeter)).stream().map(param->param.trim()).collect(Collectors.toList());
}
spring 中一个接口多个实现类注入
1. 首先, Interface1 接口有两个实现类 Interface1Impl1 和 Interface1Impl2
Interface1 接口:
package com.example.service;
/**
* Created by liuzh on 2018-05-29.
* 接口1
*/
public interface Interface1 {
void fun1();
}
以下是接口的两个实现类,请注意@service注解的使用方式,这里给每个实现类标注了不同的名称,方便在@Resource注入时区别注入
Interface1 接口实现类1:
package com.example.service.impl;
import com.example.service.Interface1;
import org.springframework.stereotype.Service;
/**
* Created by liuzh on 2018-05-29.
*/
@Service("s1")
public class Interface1Impl1 implements Interface1 {
@Override
public void fun1() {
System.out.println("接口1实现类 ...");
}
public void fun2(){
System.out.println("接口1实现类1 fun2 ...");
}
}
20
Interface1 接口实现类2:
package com.example.service.impl;
import com.example.service.Interface1;
import org.springframework.stereotype.Service;
/**
* Created by liuzh on 2018-05-29.
*/
@Service("s2")
public class Interface1Impl2 implements Interface1 {
@Override
public void fun1() {
System.out.println("接口1实现类 ...");
}
public void fun2(){
System.out.println("接口1实现类2 fun2 ...");
}
}
2. 通过 @Autowired 和 @Qualifier 配合注入
@Autowired
@Qualifier("interface1Impl1")
Interface1 interface1; //正常启动
3. 使用@Resource注入,根据默认类名区分
@Resource(name = "interface1Impl1")
Interface1 interface1; //正常启动
4. 使用@Resource注入,根据@Service指定的名称区分
@Resource(name = "s1")
Interface1 interface1; //正常启动
使用@Resource注入,根据@Service指定的名称区分,可以避免多个实现类在不同包下,但是类名相同的情况。
最近项目需要添加 SSO ,webservice 也难逃一劫,所以这里用上 拦截器做 SSO 拦截
package com.middleplugin.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SSOInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
return false;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
package com.middleplugin.interceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@EnableWebMvc
@Configuration
public class AdapterConfig extends WebMvcConfigurerAdapter {
@Bean
SSOInterceptor ssoInterceptor(){
return new SSOInterceptor();
}
public void addInterceptors(InterceptorRegistry registry){
// 多个拦截器组成一个拦截器链
// addPathPatterns 用于添加拦截规则
// excludePathPatterns 用户排除拦截
registry.addInterceptor( ssoInterceptor()).addPathPatterns("/**");
super.addInterceptors(registry);
}
}
构造访问请求命令, 便于 工作
for query_string in ['colon cancer, NTRK1, NTRK2, NTRK3',
'colon cancer NTRK1 NTRK2 NTRK3',
'breast cancer patients who are at least 18 years old',
'breast cancer 18 years or older','breast cancer patients age 70 to 80',
'pancreatic cancer patients age 80 or older',
'patients with breast cancer, gene mutation NTRK1, NTRK2, NTRK3, and do not have larotrectinib',
'patients with breast cancer, gene mutation NTRK1, NTRK2, NTRK3, and no larotrectinib',
'colon cancer patient with overall survival over 18 months','colon cancer patient with overall survival more than 18 months',
'colon cancer patient with karnofsy > 60%','colon cancer patient with karnofsy greater than 60%']:
template = """ curl -v -H 'Content-Type: application/json' -X POST -d '$"query_string":"{}"@' http://localhost:5000/v1/nlpquery/get """.format(query_string)
template = template.replace(r'$','{').replace(r'@','}')
print(template)
如果有来生,一个人去远行,看不同的风景,感受生命的活力。。。