具有Spring数据和基本身份验证的Solr 7无法正常工作

时间:2019-03-04 06:19:19

标签: solr spring-data

@SpringBootApplication 公共类SpringDataSolarApplication {

public static void main(String[] args) {
    SpringApplication.run(SpringDataSolarApplication.class, args);
}

@Bean
SolrTemplate solrTemplate() {
  return new SolrTemplate(solrClientFactory());
}

@Bean
SolrClientFactory solrClientFactory() {
  Credentials credentials = new UsernamePasswordCredentials("solr", "SolrRocks");
  return new HttpSolrClientFactory(solrClient(), credentials , "BASIC");
}

@Bean
SolrClient solrClient() {

  return new HttpSolrClient.Builder("http://localhost:8983/solr").build();
}

}

公用接口EmployeeRepository扩展了SolrCrudRepository {

Employee findByName(String name);

}

@RestController 公共类EmployeeController {

@Autowired
private EmployeeRepository repository;

@PostConstruct
public void addEmployees() {
    List<Employee> employees = new ArrayList<>();
    employees.add(new Employee("373", "Basant", new String[] { "Bangalore", "BTM" }));
    employees.add(new Employee("908", "Santosh", new String[] { "Hyderbad", "XYZ" }));
    employees.add(new Employee("321", "Sagar", new String[] { "Pune", "PQR" }));
    repository.saveAll(employees);

}

@GetMapping("/getALL")
public Iterable<Employee> getEmployees() {
    return repository.findAll();
}

@GetMapping("/getEmployee/{name}")
public Employee getEmployeeByName(@PathVariable String name) {
    return repository.findByName(name);
}

}

getALL操作运行正常,但保存操作因此错误而失败。请帮助

原因:org.apache.http.client.NonRepeatableRequestException:无法使用不可重复的请求实体重试请求。     在org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:225)〜[httpclient-4.5.7.jar:4.5.7]     在org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)〜[httpclient-4.5.7.jar:4.5.7]     在org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)〜[httpclient-4.5.7.jar:4.5.7]     在org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)〜[httpclient-4.5.7.jar:4.5.7]     在org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)〜[httpclient-4.5.7.jar:4.5.7]     ...省略了63个共同的框架

1 个答案:

答案 0 :(得分:1)

遇到相同的问题,并通过扩展HttpSolrClient并以Solr docs中提到的推荐方式应用相同的后端方法来解决,但未从构造函数中获取凭据,而是在每个请求上都未设置

class CustomSolrClient extends HttpSolrClient {
    @Nullable
    private final String username;
    @Nullable
    private final String password;

    CustomSolrClient(Builder builder, String username, String password) {
        super(builder);
        this.username = username;
        this.password = password;
    }

    @Override
    public NamedList<Object> request(SolrRequest request, ResponseParser processor, String collection) throws SolrServerException, IOException {
        HttpRequestBase method = createMethod(request, collection);
        if (username != null && password != null) {
            String userPass = username + ":" + password;
            String encoded = Base64.byteArrayToBase64(userPass.getBytes(UTF_8));
            method.setHeader(new BasicHeader("Authorization", "Basic " + encoded));
        }
        return executeMethod(method, processor, request instanceof V2Request || request.getPath().contains("/____v2"));
    }
}

并使用该代码创建bean:

    @Bean
    public SolrClient solrClient() {
        return new CustomSolrClient(new HttpSolrClient.Builder(properties.getHost()), properties.getUsername(), properties.getPassword());
    }

这似乎是一个丑陋的方法,但是如果您检查HttpSolrClientFactory sources,则实际上访问HttpClient属于Solr客户端的私有字段的丑陋程度更大。