小编Cre*_*tor的帖子

使用@pathparam和@requestmapping获取null

我使用spring-boot 1.4.3.RELEASE来创建Web服务,而在给出请求时http://localhost:7211/person/get/ram,我为id属性获取null

@RequestMapping(value="/person/get/{id}", method=RequestMethod.GET, produces="application/json")
    public @ResponseBody Person getPersonById(@PathParam("id") String id) {
        return personService.getPersonById(id);
    }
Run Code Online (Sandbox Code Playgroud)

你可以建议我,有什么我错过的吗?

spring-boot

13
推荐指数
2
解决办法
2万
查看次数

HTTP流与Java jersey客户端异常丢弃

对于我们的一个项目,我们使用java jersey客户端来使用HTTP feed流

随着客户端Feed的消耗很好,但在10分钟后,流量异常下降; 即使流生产者已启动并正在运行并生成流

这就是我的尝试;

import java.util.Date;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.ChunkedInput;

public class StreamClient {

    private static final String BOUNDARY = "\n";

    public void makeCall() {
        System.out.println("Start Time"+ new Date()) ;
        Client client = ClientBuilder.newClient();
        BasicAuth auth = new BasicAuth();
        auth.setPassword("username");
        auth.setUserName("password");
        BasicAuthentication basicAuthentication = new BasicAuthentication(auth);
        client.register(basicAuthentication);
        WebTarget target = client.target("https://localhost:7211/stream/v1/");
        Builder request = target.request(MediaType.APPLICATION_JSON);
        Response response = request.get();

        final ChunkedInput<String> chunkedInput = response.readEntity(new GenericType<ChunkedInput<String>>() { …
Run Code Online (Sandbox Code Playgroud)

java jersey jersey-client

11
推荐指数
1
解决办法
287
查看次数

Kafka 消费者没有选择提到的 Bootstrap 服务器

我正在尝试使用 SSL 实现 Kafka Consumer,在 application.yml 中提供所有必需的配置;

当我启动 Spring Boot Kafka 消费者应用程序时;消费者正在尝试连接 localhost:9092 而不是提到的 Kafka Brokers。

KafkaConfig.java

@Bean
    public ConsumerFactory<String, AvroRecord> consumerFactory() throws IOException {
        return new DefaultKafkaConsumerFactory<>(kafkaProps());
    }

    @Bean
    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, AvroRecord>>
    kafkaListenerContainerFactory() throws IOException {
        ConcurrentKafkaListenerContainerFactory<String, AvroRecord> factory =
                new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        return factory;
    }
Run Code Online (Sandbox Code Playgroud)

kafkaProps()正在加载所有 SSL 和引导服务器相关属性。值,我可以在调试模式下看到它。

应用程序.yml

kafka:
  properties:
    basic:
      auth:
        credentials:
          source: USER_INFO
          user: username
          pass: password
    enableAutoRegister: true
    max_count: 100
    max_delay: 5000
    schema:
      registry:
        url: https://schema-registry:8081
        ssl:
          truststore:
            location: <<location>>
            password: pwd
          keystore: …
Run Code Online (Sandbox Code Playgroud)

apache-kafka spring-boot spring-kafka

5
推荐指数
1
解决办法
3757
查看次数

如何为假客户端设置固定标头而不是在请求级别设置

我正在使用 feign 客户端进行服务间通信;问题是我能够在请求级别发送方法/请求标头,这意味着例如:

@FeignClient(name = "product-service", url = "https://jsonplaceholder.typicode.com/")
public interface ProductClient {

    @GetMapping("/posts")
    List<PostDTO> fetchPosts(@RequestHeaders....);

    @GetMapping("/posts/{id}")
    List<PostDTO> fetchPostsById(@RequestHeaders...., @PathVariable("id")int id);
Run Code Online (Sandbox Code Playgroud)

但由于 header 是固定的,而不是向每个请求发送相同的值;我们可以将其设置为班级级别吗?我在下面尝试过;它不工作

@FeignClient(name = "product-service", url = "https://jsonplaceholder.typicode.com/")
@Headers({
        "X-Ping: {token}"
})
public interface ProductClient {

    @GetMapping("/posts")
    List<PostDTO> fetchPosts(@RequestHeaders....);

    @GetMapping("/posts/{id}")
    List<PostDTO> fetchPostsById(@RequestHeaders...., @PathVariable("id")int id);
Run Code Online (Sandbox Code Playgroud)

使用 API 或示例纠正我。

java spring-boot feign

1
推荐指数
1
解决办法
1万
查看次数

从日期字符串转换为日期时的日期解析异常

如何在java中解析这个日期字符串

“2020-06-12T00:00:00.000+00:00”

我尝试了以下代码:

public static String convertToStandardDateString(String date) {
        // 2020-06-12T00:00:00.000+00:00
        String resDate = null;
        try {
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+'X");
            Date parsedDate = sdf.parse(date);
            resDate = sdf.format(parsedDate);
        } catch (Exception e) {

        }
        return resDate;
    }
Run Code Online (Sandbox Code Playgroud)

我得到ParsingException了上面的代码。

java date

0
推荐指数
1
解决办法
51
查看次数

我们如何有效地编码空检查 Java 8

新到Java 8风格;

我们可以通过在 Java 8 API 中包含所有空检查来为以下语句编写代码有多有效

search.getResource()
                        .getResults().get(0).getCustomer().getPhoneNumber()
Run Code Online (Sandbox Code Playgroud)

我试过如下:(我觉得到处都是 Optional 有点奇怪)

List<Result> results = search.getResource().getResults();

                Optional<Result> optionalResult =  Optional.of(results).orElse(new ArrayList<>()).stream().findFirst();
                if(optionalResult.isPresent() && Optional.of(optionalResult.get().getCustomer()).isPresent()) {
                    Source source = Optional.of(optionalResult.get().getCustomer()).get();
                    Optional<List<Customer>> customers = Optional.of(source.getCustomers());
                    if(customers.isPresent() && customers.get().stream().findFirst().isPresent() &&
                            Optional.of(customers.get().stream().findFirst().get().getPhoneNumber()).isPresent())
                        dest.setNumber(Integer.parseInt(customers.get().stream().findFirst().get().getPhoneNumber())));
                }
Run Code Online (Sandbox Code Playgroud)

你能不能给我建议一个更好的方法。谢谢!

java java-8

0
推荐指数
1
解决办法
72
查看次数