小编Pac*_*ver的帖子

尝试通过 Spring Boot Rest 使用 Jackson 验证 JSON

我正在尝试使用 Spring Boot 创建一个 RESTful Web 服务,它将接收 JSON 并使用 Jackson 对其进行验证。

这是 RESTful Web 服务:

import java.util.Map;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.google.gson.Gson;

@RestController
@RequestMapping("/myservice")
public class ValidationService {    

    @RequestMapping(value="/validate", method = RequestMethod.POST)
    public void validate(@RequestBody Map<String, Object> payload) throws Exception {
        Gson gson = new Gson();
        String json = gson.toJson(payload); 
        System.out.println(json);
        boolean retValue = false;

        try {
            retValue = Validator.isValid(json);
        } 
        catch(Throwable t) {
            t.printStackTrace();
        }
        System.out.println(retValue);

    }
}
Run Code Online (Sandbox Code Playgroud)

这是验证器的代码:

import …
Run Code Online (Sandbox Code Playgroud)

java json spring-mvc jackson spring-boot

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

如何使用 Python 将 JSON 中的值替换为 RegEx 在文件中找到的值?

假设文件系统中有一个文件,其中包含以$.

例如

<ul>
    <li>Name: $name01</li>
    <li>Age: $age01</li>
</ul>
Run Code Online (Sandbox Code Playgroud)

我能够通过正则表达式获取值:

#!/usr/bin/env python 
import re

with open("person.html", "r") as html_file:
    data=html_file.read()   
list_of_strings = re.findall(r'\$[A-Za-z]+[A-Za-z0-9]*', data)
print list_of_strings
Run Code Online (Sandbox Code Playgroud)

这会将值打印到列表中:

[$name01, $age01]
Run Code Online (Sandbox Code Playgroud)

现在,我将 JSON 示例负载发送到我的 web.py 服务器,如下所示:

curl -H "Content-Type: application/json" -X POST -d '{"name":"Joe", "age":"25"}' http://localhost:8080/myservice
Run Code Online (Sandbox Code Playgroud)

我能够像这样获得这些值:

import re
import web
import json

urls = (
    '/myservice', 'Index',
)

class Index:
    def POST(self):
        data = json.loads(web.data())

        # Obtain JSON values based on specific keys
        name = data["name"]
        age = data["age"]
Run Code Online (Sandbox Code Playgroud)

问题): …

python regex json web.py python-2.7

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

如何根据指定长度格式化带有空格的字符串?

尝试在 Java 中创建一个方法,通过根据长度拉伸缓冲区的内容(通过放置适当数量的空格)来格式化字符串。因此,根据给定的特定长度,字符串的第一个字符位于第一个索引中,最后一个字符位于实际的最后一个索引本身。

public static String format(String sentence, int length) {
    if (sentence.length() >= length) {
        return sentence;
    }
    StringBuilder sb = new StringBuilder();

    String[] words = sentence.split("\\s+");

    int usedCharacters = 0;

    for (String word : words) {
        usedCharacters += word.length();
    }

    int emptyCharacters = length - usedCharacters;
    int spaces = emptyCharacters / words.length - 1;

    for (String word : words) {
        sb.append(word);
        for (int i = 0; i <= spaces; i++) {
            sb.append(" ");
        }
    }
    return sb.toString();
} …
Run Code Online (Sandbox Code Playgroud)

java string

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

PlantUML:如何在图例、页脚和标题间距之间插入换行符或 &lt;br&gt;?

使用 PlantUML 生成包含图例和页脚创建了一个序列图。

页脚包含我公司的名称和版权日期。

图例非常接近页脚,我需要在图例和页脚之间插入一个新行(或 a<br>或 a )。<p>另外,我的标题似乎在标题和图例之间有很大的空间。

我的 puml DSL 文件:


@startuml
skinparam Shadowing false
title __Dating API Sequence Diagram__\n
caption \nVersion 1.0 - 6/26/2020 (Draft)\n
autonumber
activate DatingApp
DatingApp -> DatingRestController: hitExternalApi()
activate DatingRestController
DatingRestController -> DatingService: processService()
activate DatingService
DatingService -> DatingService: findProfile()
activate DatingService #90EE90
DatingService -> DatingService: doSomething()
DatingService -> DatingService: doSomethingElse()
deactivate DatingService
DatingService -> DatingRestController: return retValue
DatingRestController -> DatingApp: jsonPayload
deactivate DatingRestController
deactivate DatingApp
legend bottom right
Legend …
Run Code Online (Sandbox Code Playgroud)

uml sequence-diagram plantuml

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

如何使用Reddit的API登录?

我试图通过使用其API页面上列出的Reddit API - POST登录端点来访问我的Reddit用户帐户.

我试过这个:

curl -i -X POST -d '{"user":"myusername", "passwd":"mypassword", "rem":"true" }' http://www.reddit.com/api/login
Run Code Online (Sandbox Code Playgroud)

但它说错误的密码(我使用相同的凭据登录到网站,所以我不知道什么是错的):

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
{
    "jquery": 
             [[0, 1, "call", ["body"]], [1, 2, "attr", "find"], 
              [2, 3, "call", [".status"]], [3, 4, "attr", "hide"], 
              [4, 5, "call", []],  [5, 6, "attr", "html"], 
              [6, 7, "call", [""]], [7, 8, "attr", "end"], 
              [8, 9, "call", []], [0, 10, "attr", "find"], 
              [10, 11, "call", [".error.WRONG_PASSWORD.field-passwd"]], 
              [11, 12, "attr", "show"], [12, 13, "call", []], 
              [13, 14, …
Run Code Online (Sandbox Code Playgroud)

curl http reddit http-post

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

如何使用GoClipse调试Go程序?

在OS X Mavericks上使用Go(go1.3 darwin/amd6)和GoClipse 0.8 ...

运行调试器时遇到了问题(在设置断点之后),因此我搜索了Stack Overflow以及其他Internet,发现我需要安装gdb.

按照以下说明(到T)(通过HomeBrew安装gdb):

http://ntraft.com/installing-gdb-on-os-x-mavericks/

现在,当我通过Eclipse的调试器放置一个断点和Run my go程序时,它会逐步执行汇编代码而不是Go代码:

例如

在我的go程序中设置了这一行的断点:

responses := [] *HttpResponse{}
Run Code Online (Sandbox Code Playgroud)

当我运行调试器时,它打开了一个名为的文件:

rt0_darwin_amd64.s

并且它所设置的代码行是:

MOVQ    $_rt0_go(SB), AX
Run Code Online (Sandbox Code Playgroud)

当我试图"跳过"我的代码时,它通过这些汇编文件继续这样做......

我不知道汇编(并且不认为我有时间学习它)...有没有一种使用Eclipse调试器调试Go程序的简单方法?

eclipse debugging gdb go goclipse

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

如何在IntelliJ IDEA中使用Gradle运行main方法?

是IntelliJ IDEA的新手(使用2017.1.3)和gradle ...

Java文件:

package com.example;

public class HelloGradle {

    public static void main(String[] args) {
        System.out.println("Hello Gradle!");
    }
}
Run Code Online (Sandbox Code Playgroud)

的build.gradle:

group 'com.example'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'application'

mainClassName = "HelloGradle"

sourceCompatibility = 1.8

repositories {
    maven {
        url("https://plugins.gradle.org/m2/")
    }
}

task(runMain, dependsOn: 'classes', type: JavaExec) {
    main = 'com.example.HelloGradle'
    classpath = sourceSets.main.runtimeClasspath
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
Run Code Online (Sandbox Code Playgroud)

当我单击Gradle Projects窗口中的运行任务时,我得到以下内容:

在此输入图像描述

如何设置Gradle或IntelliJ IDEA以将主方法中的内容打印到IntelliJ IDEA的控制台视图?

真的不明白为什么Console视图没有弹出(从IntelliJ IDEA内部,以及为什么它没有告诉我错误是什么)...

java intellij-idea gradle

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

使用Elastic Search 5.5.0获得最佳性能时如何正确关闭原始RestClient?

使用Spring Boot 1.5.4.RELEASE Microservice使用ElasticSearch提供的低级Rest Client连接到ElasticSearch 5.5.0实例.

的pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
</parent>

<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- Elasticsearch -->
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>5.5.0</version>
    </dependency>

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>transport</artifactId>
        <version>5.5.0</version>
    </dependency>

    <!-- Apache Commons -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.6</version>
    </dependency>

    <!-- Jackson -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.9</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.9</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.8.9</version>
    </dependency>

    <!-- Log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <!-- JUnit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version> …
Run Code Online (Sandbox Code Playgroud)

java rest-client elasticsearch

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

如何转换UTC日期字符串并删除Java中的T和Z?

我正在使用Java 1.7。

尝试转换:

2018-05-23T23:18:31.000Z 
Run Code Online (Sandbox Code Playgroud)

进入

2018-05-23 23:18:31
Run Code Online (Sandbox Code Playgroud)

DateUtils类:

public class DateUtils {

    public static String convertToNewFormat(String dateStr) throws ParseException {
        TimeZone utc = TimeZone.getTimeZone("UTC");
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        sdf.setTimeZone(utc);
        Date convertedDate = sdf.parse(dateStr);
        return convertedDate.toString();
    }
}
Run Code Online (Sandbox Code Playgroud)

尝试使用它时:

String convertedDate = DateUtils.convertToNewFormat("2018-05-23T23:18:31.000Z");
System.out.println(convertedDate);
Run Code Online (Sandbox Code Playgroud)

得到以下异常:

Exception in thread "main" java.text.ParseException: Unparseable date: "2018-05-23T23:22:16.000Z"
   at java.text.DateFormat.parse(DateFormat.java:366)
   at com.myapp.utils.DateUtils.convertToNewFormat(DateUtils.java:7)
Run Code Online (Sandbox Code Playgroud)

我可能做错了什么?

有没有更简单的方法(例如Apache Commons lib)?

java utc parseexception java-7 datetime-parsing

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

Spring Boot JPA 元模型不能为空!尝试运行 JUnit / 集成测试时

我在基于 maven 的项目中使用 Spring Boot、JUnit 4 和 Mockito 来测试我的 Spring Boot 微服务 REST API。

因此,在启动时,DataInserter 类从 owner.json 和 cars.json 加载数据。

通常,通过我的 REST 调用,一切正常,但似乎我在设置单元测试和集成测试方面有问题。

项目结构:

myapi
? 
??? pom.xml
?
??? src
    ??? main
    ?   ? 
    ?   ??? java
    ?   ?   ?
    ?   ?   ??? com
    ?   ?       ?  
    ?   ?       ??? myapi
    ?   ?           ? 
    ?   ?           ??? MyApplication.java
    ?   ?           ?
    ?   ?           ??? bootstrap
    ?   ?           ?   ?
    ?   ?           ?   ??? DataInserter.java
    ?   ?           ?   
    ?   ? …
Run Code Online (Sandbox Code Playgroud)

java junit mockito spring-data-jpa spring-boot

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