小编Vel*_*aga的帖子

如何从第1页开始配置Spring引导分页,而不是0

引导(1.4.0)分页的"可分页".它没有任何问题.但默认情况下,页面值从"0"开始,但在前端页面值从"1"开始.那么是否有任何增加值的标准方法而不是手动增加代码中的页码?

public Page<Device> find(DeviceFindCommand deviceFindCommand, Pageable pageable){
//page = 0 //Actual is 0, Expected increment by 1. 
}
Run Code Online (Sandbox Code Playgroud)

任何帮助都应该是值得的.

在实施Alan回答有以下问题后,

1)仍然我能够访问返回第一页的零页面(我不知道这是不是问题,但我希望得到更好的清晰度).

HTTP://本地主机:8180/API/V1 /书籍/页= 3&大小= 2

响应

{
    "content": [
{
  "id": "57da9eadbee83fb037a66029",
  .
  .
  .
}{
.
.
.
}
],
    "last": false,
    "totalElements": 5,
    "totalPages": 3,
    "size": 2,
    "number": 2, //strange always getting 1 less than page number.
    "sort": null,
    "first": true,
    "numberOfElements": 2
}
Run Code Online (Sandbox Code Playgroud)

2)"数字":2,在响应中总是得到一个小于页码.它应该返回当前页面索引.

spring pagination spring-mvc spring-data-jpa spring-boot

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

如何比较JSON文档并通过Jackson或Gson返回差异?

我正在使用spring-boot开发后端服务。有一种方案可以比较2个bean(一个是DB对象,另一个是客户请求的对象),然后返回“新元素”,“修改后的元素”,如果没有变化,则返回false。2豆格式如下

"sampleList":{
     "timeStamp":"Thu, 21 Jun 2018 07:57:00 +0000",
     "id":"5b19441ac9e77c000189b991",
     "sampleListTypeId":"type001",
     "friendlyName":"sample",
     "contacts":[
        {
           "id":"5b05329cc9e77c000189b950",
           "priorityOrder":1,
           "name":"sample1",
           "relation":"Friend",
           "sampleInfo":{
              "countryCode":"91",
              "numberType":"MOBILE",
              "numberRegion":"IN"
           }
        },
        {
           "id":"5b05329cc9e77c000189b950",
           "priorityOrder":1,
           "name":"sample2",
           "relation":"Friend",
           "sampleInfo":{
              "countryCode":"91",
              "numberType":"MOBILE",
              "numberRegion":"IN"
           }
        }
     ]
  }
Run Code Online (Sandbox Code Playgroud)

我已经在Java上浏览了有关此方案的bean比较的Internet,但是找不到任何更简单的解决方案,但是找到了一些很酷的JSON解决方案。我可以看到GSON的一些解决方案,但它不会返回包含“新元素”和“变更元素”的客户端对象。有什么方法可以用JSON或JAVA返回更新和修改的元素?您的帮助应该是可观的。对于我来说,即使是提示也将是一个很好的开始。

java json jackson gson

8
推荐指数
2
解决办法
7786
查看次数

如何将jaxb打印到java对象到logger中

嗨,通过以下链接,我能够将JAXB转换为java对象,并使用下面的语句在控制台中打印它.

http://www.mkyong.com/java/jaxb-hello-world-example/

jaxbMarshaller.marshal(customer, System.out);
Run Code Online (Sandbox Code Playgroud)

但我想在记录器中打印输出.

ex)log.info(客户)或log.debug(客户)

我正在使用Apache log4j.有没有人有任何想法?

java apache logging jaxb java-ee

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

Jersey 客户端使用 x-www-form-urlencoded 发布请求失败

嗨,我正在使用 Glassfish jersey-client 从 REST URL 获取 oauth-Token。我可以通过邮递员客户端和 CURL 获取令牌,请找到下图以供参考,

$ curl 'https://sample.com/oauth2/token' -X POST -d'g
rant_type=samples&id=2ZwqWBdksfjkads6Q8yNW3s58LNeOMucJeb&s                                                                                                                
ecret=dkfflJTZOqA1GCEH&scope=GROUP'
Run Code Online (Sandbox Code Playgroud)

但无法通过代码实现,

<dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.22.2</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.22.2</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

我正在使用以下代码来获取令牌

Form form = new Form();
        form.param("grant_type", "samples");
        form.param("id", "2ZwqWBdksfjkads6Q8yNW3s58LNeOMucJeb");
        form.param("secret", "HGoslJTZOqA1GCEH");
        form.param("scope", "dkfflJTZOqA1GCEH");
JerseyClientBuilder jerseyClientBuilder = new JerseyClientBuilder()
            .register(new LoggingFilter());
    JerseyWebTarget jerseyWebTarget =      jerseyClientBuilder.build().target(hostname);
        response = jerseyWebTarget.request().accept(MediaType.APPLICATION_FORM_URLENCODED).post(Entity.form(form));
Run Code Online (Sandbox Code Playgroud)

继续获取 StatusCode=406(不可接受)作为响应。我应该正确传递 URL 参数吗?

如果有人给我一个提示来解决这个问题,我将不胜感激。

java rest jersey jersey-client jersey-2.0

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

Spring-boot-starter RabbitMQ全局错误处理

我使用spring-boot-starter-amqp 1.4.2.Producer和consumer工作正常,但有时传入的JSON消息的语法不正确.这导致以下(正确)异常:

org.springframework.amqp.rabbit.listener.ListenerExecutionFailedException: Listener threw exception
Caused by:  org.springframework.amqp.support.converter.MessageConversionException: Failed to convert Message content
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token...
Run Code Online (Sandbox Code Playgroud)

将来我可能面临更多例外.所以我想配置一个全局错误处理程序,这样如果任何一个消费者都有任何异常我可以全局处理它.

注意:在这种情况下,消息根本没有到达消费者.我希望在整个消费者中处理这些类型的异常.

请找到以下代码:

RabbitConfiguration.java

@Configuration
@EnableRabbit
public class RabbitMqConfiguration {

    @Autowired
    private CachingConnectionFactory cachingConnectionFactory;

    @Bean
    public MessageConverter jsonMessageConverter()
    {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    @Primary
    public RabbitTemplate rabbitTemplate()
    {
        RabbitTemplate template = new RabbitTemplate(cachingConnectionFactory);
        template.setMessageConverter(jsonMessageConverter());
        return template;
    }

}
Run Code Online (Sandbox Code Playgroud)

消费者

@RabbitListener(
        id = "book_queue",
        bindings = @QueueBinding(
                value = @Queue(value = "book.queue", durable …
Run Code Online (Sandbox Code Playgroud)

rabbitmq spring-rabbit spring-boot rabbitmq-exchange spring-rabbitmq

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

如何在 spring-ws 中解析 SoapFaultClientException

我正在使用 spring-ws-2.3.1,在为 web 服务创建客户端时,有时我收到SoapFaultClientException,如下所示,

<SOAP-ENV:Body>
  <SOAP-ENV:Fault>
     <faultcode>SOAP-ENV:Server</faultcode>
     <faultstring>There was a problem with the server so the message could not proceed</faultstring>
     <faultactor>InvalidAPI</faultactor>
     <detail>
        <ns0:serviceException xmlns:ns0="http://www.books.com/interface/v1">
           <ns1:messageId xmlns:ns1="http://www.books.org/schema/common/v3_1">5411</ns1:messageId>
           <ns1:text xmlns:ns1="http://www.books.org/schema/common/v3_1">Locale is invalid.</ns1:text>
        </ns0:serviceException>
     </detail>
  </SOAP-ENV:Fault>
Run Code Online (Sandbox Code Playgroud)

我正在尝试获取 ServiceException 的“messageId”和“Text”,但我不能。请在下面找到代码,

catch (SoapFaultClientException ex) {
        SoapFaultDetail soapFaultDetail = ex.getSoapFault().getFaultDetail(); // <soapFaultDetail> node
        // if there is no fault soapFaultDetail ...
        if (soapFaultDetail == null) {
            throw ex;
        }
        SoapFaultDetailElement detailElementChild = soapFaultDetail.getDetailEntries().next();
        Source detailSource = detailElementChild.getSource();
        Object detail = webServiceTemplate.getUnmarshaller().unmarshal(detailSource);
        System.out.println("Detail"+detail.toString());//This object prints …
Run Code Online (Sandbox Code Playgroud)

java spring web-services spring-ws soapfault

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

Java8流来查找多个值并返回布尔值

我使用java-8来查找自定义对象列表中的值.对于前)

List<Employee> employees = Arrays.asList(
        new Employee("Sachin Tendulkar", 41),
        new Employee("Sachin Tendulkar", 36),
        new Employee("MS Dhoni", 34),
        new Employee("Rahul Dravid", 40),
        new Employee("Lokesh Rahul", 25),
        new Employee("Sourav Ganguly", 40)
);
Run Code Online (Sandbox Code Playgroud)

要在列表中查找值,我可以使用以下查询,

boolean isPresent = employees.stream()
            .allMatch(employee -> (equalsIgnoreCase(employee.getName(),"Sachin Tendulkar") && equalsIgnoreCase(employee.getAge(),"36")));
Run Code Online (Sandbox Code Playgroud)

以上工作正常.但我想找到36岁的"Sachin Tendulkar"和40岁的"Rahul Dravid".如何在Java8流中实现这一目标.我尝试了多个"allMatch",但这不起作用.

任何提示都会很明显.

java java-8 java-stream

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

如何通过反射或任何其他方式手动触发Javax验证?

我正在使用validation-api-2.0.1.Finalhibernate-validator-6.0.13.Final。我想对以下情况进行验证,

我创建了一个自定义验证来验证 List<Map<String,Object>>

BookInfo.java

@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(
        validatedBy = {BookInfoValidator.class}
)
public @interface BookInfo {
    String message() default "Should not be empty";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}
Run Code Online (Sandbox Code Playgroud)

BookInfoValidator.java

public class BookInfoValidator implements ConstraintValidator<ValidateUserInfo, List<Map<String, Object>>> {

    private final ContentRepositoryClient contentRepository;

    public ValidateUserInfoValidator(ContentRepositoryClient contentRepository) {
        this.contentRepository = contentRepository;
    }

    @Override
    public void initialize(ValidateUserInfo constraintAnnotation) {

    }

    @Override
    public boolean isValid(List<Map<String,Object>> map, ConstraintValidatorContext constraintValidatorContext) {
        //In …
Run Code Online (Sandbox Code Playgroud)

java validation spring hibernate-validator bean-validation

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

将 HOCON 字符串转换为 Java 对象

我的 Web 服务之一返回 Java 字符串下方:

[
  {
    id=5d93532e77490b00013d8862, 
    app=null,
    manufacturer=pearsonEducation, 
    bookUid=bookIsbn, 
    model=2019,
    firmware=[1.0], 
    bookName=devotional, 
    accountLinking=mandatory
  }
]
Run Code Online (Sandbox Code Playgroud)

我有上述字符串的等效 Java 对象。我想将上述 java 字符串类型转换或转换为 Java 对象。

我无法对它进行类型转换,因为它是一个字符串,而不是一个对象。所以,我试图将 Java 字符串转换为 JSON 字符串,然后我可以将该字符串写入 Java 对象,但没有运气出现invalid character "="异常。

您可以更改 Web 服务以返回 JSON 吗?

那是不可能的。他们没有改变他们的合同。如果他们返回 JSON,那就太容易了。

java parsing json pojo hocon

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

在Spring-Boot-RabbitMQ中处理连接

嗨,我正在开发Spring-boot-RabbitMQ版本1.6。在开发应用程序时,我遇到的查询很少。阅读文档并浏览了其他堆栈溢出问题,但是我无法弄清楚几件事(可能是因为我的内存不好)。如果有人回答我的问题,那就太好了。

1)目前我有4个生产者和4个生产者。生产者可能产生数百万条消息或事件,因此对生产者和消费者使用单个连接将阻止消费者消费消息。生产者和消费者,这样它们就不会阻塞并且会改善性能。我对这种方法正确吗?

2)我正在使用CachingConnectionFactory来通过SimpleRabbitListenerContainerFactory创建连接,同时调用该工厂是否会为我们返回新的连接,因此如果我们使用CachingConnectionFactory,我们是否真的需要为Producer和Consumer编写一个单独的连接工厂。请在下面找到我的

1)配置类

@Configuration
@EnableRabbit
public class RabbitMqConfiguration{

@Autowired
private CachingConnectionFactory cachingConnectionFactory;

@Value("${concurrent.consumers}")
public int concurrent_consumers;

@Value("${max.concurrent.consumers}")
public int max_concurrent_consumers;

 @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(cachingConnectionFactory);
        factory.setConcurrentConsumers(concurrent_consumers);
        factory.setMaxConcurrentConsumers(max_concurrent_consumers);
        factory.setMessageConverter(jsonMessageConverter());
        return factory;
    }

@Bean
public MessageConverter jsonMessageConverter()
{
    final Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
    return converter;
}

}
Run Code Online (Sandbox Code Playgroud)

2)生产者阶层

@Configuration
public class TaskProducerConfiguration extends RabbitMqConfiguration {

@Value("${queue1}")
public String queue1;

@Value("${queue2}")
public String queue2;

@Value("${queue3}")
public String …
Run Code Online (Sandbox Code Playgroud)

spring-rabbit spring-amqp spring-rabbitmq

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