小编Pac*_*ver的帖子

尝试使用Spring Boot REST从POST读取JSON字符串

我使用最新版本的Spring Boot通过Restful Web Service读取示例JSON ...

这是我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"       
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
         http://maven.apache.org/maven-v4_0_0.xsd"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>myservice</artifactId>
    <version>0.1.0</version>

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

    <properties>
        <java.version>1.7</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <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>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-webmvc</artifactId>
        </dependency>
        <dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
        <repository>
            <id>org.jboss.repository.releases</id>
            <name>JBoss Maven Release Repository</name>
            <url>https://repository.jboss.org/nexus/content/repositories/releases</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>

</project>
Run Code Online (Sandbox Code Playgroud)

这是我的网络服务代码:

import org.springframework.web.bind.annotation.RequestBody; …
Run Code Online (Sandbox Code Playgroud)

java rest json spring-mvc spring-boot

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

在Mac OS X上安装了Python 3,但它仍然是Python 2.7

我目前在MacBook Pro上运行OS X Yosemite(10.10.2)...默认情况下,Apple在Yosemite上运行Python 2.7.6.

刚刚下载并运行了Python 3的这个安装程序: python-3.4.3-macosx10.6.pkg

当我打开终端并输入时python,会出现这种情况:

Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Run Code Online (Sandbox Code Playgroud)

问题(S):

  1. 有谁知道Python 3.4.3解释器的安装位置?
  2. 在将全局环境变量(如PYTHON_HOME)设置为已安装的Python 3.4.3的位置之前,是否需要卸载Python 2.7.3(如果是这样,我该怎么做)?

python python-2.7 python-3.x osx-yosemite

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

如何在Oracle SQL Developer中增加缓冲区大小来查看所有记录?

如何在Oracle SQL Developer中增加缓冲区大小以查看所有记录(默认情况下似乎设置了某个限制)?任何屏幕截图和/或提示都将非常有用.

sql oracle oracle11g oracle-sqldeveloper

33
推荐指数
8
解决办法
12万
查看次数

使用Jackson将Map转换为JSON

如何使用Jackson将Map转换为有效的JSON?

我是通过Spring Boot REST Post方法使用谷歌的GSON做的...

这是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);
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,当我使用它调用它时:

curl -H "Accept: application/json" -H "Content-type: application/json" \
-X POST -d '{"name":"value"}' http://localhost:8080/myservice/validate
Run Code Online (Sandbox Code Playgroud)

收到以下stdout(这正是我想要的):

{"name":"value"}
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来使用杰克逊而不是谷歌的Gson和/或我完全以错误的方式解决这个问题?

java spring json jackson gson

32
推荐指数
3
解决办法
8万
查看次数

使用Java Reflection访问测试用例中的受保护方法

我试图使用Java Reflection获取和调用驻留在不同类中的受保护方法以及不同的包.

包含受保护方法的类:

package com.myapp;

public class MyServiceImpl {

   protected List<String> retrieveItems(String status) {
         // Implementation
   }
}
Run Code Online (Sandbox Code Playgroud)

通话课程:

package xxx.myapp.tests;

import com.myapp.MyServiceImpl;

public class MyTestCase {

    List<String> items;

    public void setUp() throws Exception {

         MyServiceImpl service = new MyServiceImpl();
         Class clazz = service.getClass();

         // Fails at the next line:
         Method retrieveItems = clazz.getDeclaredMethod("retrieveItems");

         // How to invoke the method and return List<String> items?
         // tried this but it fails?
         retrieveItems.invoke(clazz, "S");
    }
}
Run Code Online (Sandbox Code Playgroud)

编译器抛出此异常:

java.lang.NoSuchMethodException: com.myapp.MyServiceImpl.retrieveItems()
Run Code Online (Sandbox Code Playgroud)

java reflection junit protected

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

基于开源的规则引擎Java或Python

我正在寻找一个用Java或Python编写的规则引擎,它支持以下功能:

  1. 决策表
  2. 轻松创建简单的业务规则(最好由非技术人员)
  3. SOAP/REST支持

已经排除了Drools(非常笨重,对非技术用户不友好).

到目前为止候选人是:

  1. Nebri OS(Python/Django)
  2. 简易规则(Java)
  3. n-cube(基于Groovy)

阅读Easy Rules不支持前向和后向链接,以及广度优先和深度优先搜索策略.

它只执行一次所有规则.

任何人都可以建议一个基于开源的规则引擎,用Java或Python编写?

感谢您抽时间阅读.

python java rule-engine business-rules

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

Spring Security OAuth - 未为Null资源配置提供程序管理器

我试图使用Spring Secruity的OAuth API从外部发布的API获取访问令牌.

这个curl命令有效(它的内容就是我获取访问令牌所需的全部内容):

curl -X POST \
https://api.app.com/v1/oauth/token \
  -H 'content-type: application/x-www-form-urlencoded' \
  -d'grant_type=client_credentials&client_id=bcfrtew123&client_secret=Y67493012'
Run Code Online (Sandbox Code Playgroud)

运行此curl命令后,能够从外部服务获取访问令牌.

使用Spring Security OAuth API时:

<dependency>
   <groupId>org.springframework.security.oauth</groupId>
     <artifactId>spring-security-oauth2</artifactId>
     <version>2.1.1.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

设置我的SpringMVC Controller的方法如下:

@RequestMapping(value = "/getAccessToken", method = RequestMethod.POST, consumes="application/x-www-form-urlencoded")
public OAuth2AccessToken getAccessToken(@RequestParam(value="client_id", required=true) String clientId, @RequestParam(value="client_secret", required=true) String clientSecret) throws Exception {
    String tokenUri = "https://api.app.com/v1/oauth/token";

    ResourceOwnerPasswordResourceDetails resourceDetails = new ResourceOwnerPasswordResourceDetails();

    resourceDetails.setAccessTokenUri(tokenUri);
    resourceDetails.setClientId(clientId);
    resourceDetails.setClientSecret(clientSecret);
    resourceDetails.setGrantType("client_credentials");
    resourceDetails.setScope(Arrays.asList("read", "write"));

    DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();

    oauth2RestTemplate = new OAuth2RestTemplate(resourceDetails, clientContext);

    OAuth2AccessToken token = oauth2RestTemplate.getAccessToken();
    return token;
}
Run Code Online (Sandbox Code Playgroud)

当我从本地tomcat实例调用getAccessToken调用时: …

java curl oauth spring-mvc spring-security-oauth2

12
推荐指数
2
解决办法
5184
查看次数

如何使用Spring Boot微服务extern log4j.properties文件并将其作为Linux服务运行?

有一个基于Spring Boot(1.5.4.RELEASE)的微服务,我将jar部署到AWS EC实例(Linux环境).现在,我还部署了一个外部log4j.properties文件,所以我必须像这样启动微服务:

java -jar myapp.jar -Dlogging.config=/path/to/log4j.properties
Run Code Online (Sandbox Code Playgroud)

如何将此Spring Boot微服务配置为Linux服务,我可以使用这些标志启动和停止它:

sudo service myapp start | stop | status | restart
Run Code Online (Sandbox Code Playgroud)

非常感谢你.

java linux log4j spring-boot

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

Scala IDE - 播放2个Eclipse插件,不突出显示Scala HTML模板的语法

我安装了Scala IDE - Play 2插件(来自http://download.scala-ide.org/play2/nightly_3.0-M_juno_2.10-M/site/),而路径编辑器显示正确的语法高亮显示, Scala的模板没有.此外,它还不允许我在Eclipse的首选项 - >播放 - >模板 - 语法着色中"启用"语法突出显示选项.

因此,在main.scala.html和index.scala.html中没有突出显示Scala语法

我还使用http://download.scala-ide.org/nightly-scala-ide-juno-210x进行Eclipse Juno.

-詹姆士

在此输入图像描述

用鼠标右键单击上面的图像,在新的浏览器选项卡中打开,以全分辨率查看图像.

在此输入图像描述

java eclipse scala scala-ide playframework-2.0

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

使用Spring Boot Framework对基于Spring JPA的DAO进行分层的正确方法

我是Spring Boot和JPA的新手......

假设我有两个实体映射到两个表,这两个表在数据库中连接.

学生 - 1 ------ < - 课程

另外,假设已经创建并填充了数据库.

这表明一个学生有很多课程......

我的学生实体:

@Entity
public class Student {

    @OneToMany(mappedBy="student")
    private List<Courses> courses;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "Student_Id")
    private long studentId;

    @Column(name = "Student_Name")
    private String studentName;

    protected Student() { }

    // Getters & Setters
}
Run Code Online (Sandbox Code Playgroud)

我的课程实体:

@Entity
public class Course {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "Course_Id")
    private long courseId;

    @Id
    @Column(name = "Student_Id")
    private long studentId;

    @ManyToOne
    @PrimaryKeyJoinColumn(name="Student_Id", referencedColumnName="Student_Id")
    private Student student;

    @Column(name = "Course_Name")
    private …
Run Code Online (Sandbox Code Playgroud)

spring hibernate jpa hql spring-boot

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