小编T A*_*nna的帖子

AWS TransferManager uploadFileList在S3中截断文件名

我正在尝试使用TranferManager批量上传S3中的几个文件.以下是我的代码:

@GetMapping("s3/batch/upload/base64")
public void uploadBase64ToWebp() {

     List<File> fileList = new ArrayList<>();
    String rawData = "1";
    String base64Data = Base64.encodeBase64String(rawData.getBytes(StandardCharsets.UTF_8));
     byte[] data = getBinaryImageData(base64Data);
     File file = new File("1234.webp");
     try {

         FileUtils.writeByteArrayToFile(file, data);

     } catch (IOException e) {

         System.out.println(e);
     }
     fileList.add(file);
     ObjectMetadataProvider metadataProvider = new ObjectMetadataProvider() {
            public void provideObjectMetadata(File file, ObjectMetadata metadata) {

                metadata.setContentType("image/webp");
                metadata.getUserMetadata().put("filename", file.getPath());
                metadata.getUserMetadata().put("createDateTime", new Date().toString());
            }
        };
        TransferManager transferManager = TransferManagerBuilder.standard().withS3Client(amazonS3).build();
     transferManager.uploadFileList(bucketName, "school/transactions", new File("."), fileList, metadataProvider);
}

private byte[] getBinaryImageData(String image) {

        return Base64.decodeBase64(
            image …
Run Code Online (Sandbox Code Playgroud)

java amazon-s3 amazon-web-services awss3transfermanager

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

如何处理Spring Rest Web服务中的JSON解析错误

我有一个使用Spring Boot开发的休息Web服务.我能够处理由于我的代码而发生的所有异常,但是假设客户端发布的json对象与我想要对其进行反序列化的对象不兼容,我得到

"timestamp": 1498834369591,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "JSON parse error: Can not deserialize value 
Run Code Online (Sandbox Code Playgroud)

我想知道是否有一种方法,对于此异常,我可以为客户端提供自定义异常消息.我不知道如何处理这个错误.

rest spring json spring-mvc spring-boot

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

DynamoDb 查询与 Java 中的过滤器表达式

我在 AWS 中有一个名为 school-data 的 DynamoDb 表。以下是获取所有学校名称的现有代码:

private DynamoDBQueryExpression<School> createQueryBySchoolName(String schoolName) {
    String matchSchoolName = "schoolName = :schoolName";
    Map<String, AttributeValue> schoolNames = new HashMap<>();
    schoolNames.put(":schoolName", new AttributeValue().withS(schoolName));

    return new DynamoDBQueryExpression<School>()
            .withIndexName("schoolName-index")
            .withKeyConditionExpression(matchSchoolName)
            .withExpressionAttributeValues(schoolNames)
            .withConsistentRead(false);
}
Run Code Online (Sandbox Code Playgroud)

上面的查询工作正常。但是现在我需要获取具有特定学校名称及其地址的所有学校。因此,以下是表中的 3 列:

id   schoolName  details
Run Code Online (Sandbox Code Playgroud)

详细信息列中的数据如下所示:

{
"zone": "North",
"type": "Convent",
"address": {
    "id": "138",
    "street1": "123 Street",
    "street2": "456 Road"
}
}
Run Code Online (Sandbox Code Playgroud)

因此,我需要获取名称为“ABC”且地址为“123 Street”的所有学校。所以,我更新了上面的查询如下:

private DynamoDBQueryExpression<School> createQueryBySchoolName(String schoolName) {
    String matchSchoolName = "schoolName = :schoolName";
    Map<String, AttributeValue> schoolNames = new HashMap<>();
    schoolNames.put(":schoolName", new …
Run Code Online (Sandbox Code Playgroud)

java filter amazon-dynamodb

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

如何在rest get调用中将Java日期作为路径变量传递

有一个现有的 api 具有以下代码:

@GetMapping(value = "/getAmount")
public long getAmount(){

    Date d = new Date();
    long amountFetchedFromDb = callToDatabase(d);
    return amountFetchedFromDb;
}
Run Code Online (Sandbox Code Playgroud)

现在我需要更改功能如下:

@GetMapping(value = "/getAmount")
public long getAmount(){

    Date d = new Date();
    <CALL TO A NEW  REST API PASSING IT THE DATE d AND THE NEW API 
     WILL MAKE THE DB CALL AND RETURN THE AMOUNT>
    return amount;
}
Run Code Online (Sandbox Code Playgroud)

现在,我创建了一个新的休息服务,它将 java 日期作为路径变量,如下所示:

@GetMapping("/getAmount/{dateTo}")
public long getAmount(@PathVariable Date dateTo){

   long amountFetchedFromDb = callToDatabase(d);
   return amountFetchedFromDb;
}
Run Code Online (Sandbox Code Playgroud)

现在我需要测试我的新服务。如何传递下面请求中的日期:

http://localhost:8080/getAmount/

现有 …

java get date http spring-boot

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

Spring Boot Redis 将 POJO 列表存储为值

我正在使用 Redis 和 Spring boot。我使用 String 作为键,并将其对应的值作为复杂 POJO 的列表。下面是我的配置:

@Configuration
@EnableCaching
@Slf4j
public class RedisCacheConfig extends CachingConfigurerSupport {

private static final long DEFAULT_CACHE_EXPIRES = 60;

@Bean
public RedisTemplate<String, Object> redisTemplate(final RedisConnectionFactory redisConnectionFactory) {
    final RedisTemplate<String, Object> template = new RedisTemplate<>();
    setRedisTemplateConfigValues(redisConnectionFactory, template);
    return template;
}

@Bean
public CacheManager cacheManager(final RedisConnectionFactory redisConnectionFactory) {
    Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<>();

    return RedisCacheManager
            .builder(redisConnectionFactory)
            .cacheDefaults(createCacheConfiguration())
            .withInitialCacheConfigurations(cacheConfigurations).build();
}

private static RedisCacheConfiguration createCacheConfiguration() {
    return RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofSeconds(DEFAULT_CACHE_EXPIRES));
}

private <T> void setRedisTemplateConfigValues(final RedisConnectionFactory redisConnectionFactory,
                                              final …
Run Code Online (Sandbox Code Playgroud)

java json arraylist redis spring-boot

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

没有响应类型的Spring Boot RestTemplate发布

我需要调用另一个api,并且尝试使用postForEntity,如下所示:

restTemplate.postForEntity(postUrl, request, responseType)
Run Code Online (Sandbox Code Playgroud)

现在,我正在调用的api返回的是特定类型的对象,比方说CustomerDto。但是我对此对象不感兴趣。我只想发布,不希望任何回报,所以我不想在应用程序中创建这个CustomerDto类。因此,对于这种情况,在上面的语句中,我应该用responseType代替什么。如果我想要返回的对象,在responseType的情况下,我会给出CustomerDto.class,但是我什么也不想得到,那么我该怎么办?

java post resttemplate spring-boot

3
推荐指数
2
解决办法
1945
查看次数

RestTemplate 模拟给出 NullPointerException

我的服务类代码如下:

public class MyServiceImpl implements MegatillAccessService {
@Autowired
RestTemplate restTemplate;

@Value("${api.key}")
private String apiKey;

@Value("${customers.url}")
private String postUrl;

@Override
public String pushCustomerData(List<Customer> listOfcustomers, String storeId) throws MyServiceException {

Set<Customer> setOfCustomers = new HashSet<>(listOfcustomers);
    int noOfCustomersLoadedSuccessfully =0;

    MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
    headers.add("apiKey", apiKey);
    headers.add("Content-Type", "application/json");
    headers.add("storeId", storeId);
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

    for(Customer customer: setOfCustomers){
        HttpEntity<Customer> request = new HttpEntity<Customer>(customer, headers);
        CustomerDataDto customerDataDto = null;
        try {
            customerDataDto = restTemplate.exchange(postUrl, HttpMethod.POST, request, CustomerDataDto.class).getBody();
        }
        catch (HttpClientErrorException ex) {
            if (ex.getStatusCode().equals(HttpStatus.NOT_FOUND)) { …
Run Code Online (Sandbox Code Playgroud)

mockito resttemplate spring-boot

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

使用 Ubuntu .profile 和 .bashrc 时遇到问题

我是 linux 新手。我目前正在在线阅读 Kafka 的设置教程。它说将我的 kafka bin 目录的路径如下添加到我的 .profile 文件中,我做了如下操作:

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" …
Run Code Online (Sandbox Code Playgroud)

linux bash ubuntu .profile apache-kafka

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

将字符串转换为 ZonedDateTime

我需要将字符串 2020-04-15T23:00:00+0000 转换为 ZonedDateTime。我尝试了以下但他们没有工作:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss Z");
System.out.println(ZonedDateTime.parse("2020-04-15T23:00:00+0000", formatter));

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss 00:00");
System.out.println(ZonedDateTime.parse("2020-04-15T23:00:00+0000", formatter));

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss 0000");
System.out.println(ZonedDateTime.parse("2020-04-15T23:00:00+0000", formatter));
Run Code Online (Sandbox Code Playgroud)

我也使用了 ISO_LOCAL_DATE_TIME 但这没有用。有任何想法吗?

更新:

我在下面试过:

    OffsetDateTime dateTime1 = OffsetDateTime.parse("2020-04-15T23:00:00+0000",
            DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ"));
    System.out.println(dateTime1.toZonedDateTime());
Run Code Online (Sandbox Code Playgroud)

我得到的输出为 2020-04-15T23:00Z,但我需要它为 2020-04-15T23:00:00Z,最后的秒数正在修剪。

java datetime-format zoneddatetime

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