小编Jam*_*mes的帖子

使用HAL设计RESTful API - 序列化模型关系

我对REST比较陌生,但我一直在做关于RESTful应该是什么的功课.现在我正在尝试为我的模型创建一个RESTful api,它实现了一个与其他模型有关系的JSON + HAL序列化器.
python中的示例模型:

class Category(Model):
    name = CharField()
    parent = ManyToOneField(Category)
    categories = OneToManyField(Category)
    products = ManyToManyField(Product)

class Product(Model):
    name = CharField()
    price = DecimalField()
    related = ManyToManyField(Product)
    categories = ManyToManyField(Category)
Run Code Online (Sandbox Code Playgroud)

假设我们有一个类别"目录",其子类别"食物"与产品"汉堡"和"热狗"都是相关的.
第一个问题.类别和产品应该是资源,所以他们需要一个URI,我应该在我的模型实现一个URI区域,并将其存储在数据库中或以某种方式在运行时计算的话,大约多标识符(URI)是什么?
第二个问题.可发现性,在Hal格式中应该"GET /"和不同的节点返回以使api容易被自己发现.

{
  "_links":{
    "self":{
      "href":"/"
    },
    "categories":[
      {
        "href":"/catalog"
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

第三个问题.添加为属性,嵌入或链接.示例"GET/catalog/food":

{
  "_links":{
    "self":{
      "href":"/catalog/food"
    }
  },
  "name":"food",
  "parent":"/catalog",
  "categories":[],
  "products":[
    "/products/burger",
    "/products/hot-dog"
  ]
}

{
  "_links":{
    "self":{
      "href":"/catalog/food"
    },
    "parent":{
      "href":"/catalog"
    },
    "categories":[

    ],
    "products":[
      {
        "href":"/products/burger"
      }, …
Run Code Online (Sandbox Code Playgroud)

rest serialization json hateoas hal-json

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

如果有什么不是最新的,如何询问gradle

有没有办法问gradle是否有什么不是最新的并且需要建立(当时没有实际构建它)?

gradle

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

如何在java配置中获取外部定义的bean

我有一个java配置类,它使用@ImportResources注释导入xml文件.在java配置中,我想引用在xml配置中定义的bean,例如:

@Configuration
@ImportResource({
        "classpath:WEB-INF/somebeans.xml"
    }
)
public class MyConfig {
    @Bean
    public Bar bar() {
        Bar bar = new Bar();
        bar.setFoo(foo); // foo is defined in somebeans.xml
        return bar;
    }
}
Run Code Online (Sandbox Code Playgroud)

我想将somebeans.xml中定义的bean foo设置为将在java config类中创建的bar bean.我如何获得foo bean?

java spring spring-bean

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

使用 MvcUriComponentsBuilder::fromMethodCall 并将 String 作为返回类型

我想使用该MvcUriComponentsBuilder::fromMethodCall方法从我的控制器构建 URL。我通常有一个 String 返回类型(返回视图名称)和一个 Model 实例作为我的控制器方法中的方法参数,例如:

@Controller
public class MyController {
    @RequestMapping("/foo")
    public String foo(Model uiModel) {
        uiModel.addAttribute("pi", 3.1415);
        return "fooView";
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试生成一个 URL,例如:

String url = MvcUriComponentsBuilder.fromMethodCall(on(MyController.class).foo(null)).build().toUriString();
Run Code Online (Sandbox Code Playgroud)

这导致了这个异常:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class java.lang.String
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
Run Code Online (Sandbox Code Playgroud)

发生这种情况是因为 String 返回类型想要被代理,但不能作为最终类。

有什么方法可以克服这个问题?我想保留 String 作为返回类型,并从控制器方法中的参数获取模型作为输入,因为恕我直言,这比在每个控制器方法中处理 ModelAndView 实例要容易得多。

spring spring-mvc

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

如何在Eclipse工具栏中显示当前的git分支

在EGit中是否有一个插件或一些设置来显示eclipse工具栏中的当前git分支,以便在您正在工作的分支上显示它?

eclipse git egit

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

如何从heritrix抓取中排除除text/html之外的所有内容?

On:Heritrix Usecases有一个"仅存储成功的HTML页面"的用例

我的问题:我不知道如何在我的cxml文件中实现它.特别是:将ContentTypeRegExpFilter添加到ARCWriterProcessor =>将其正则表达式设置为text/html.*....示例cxml Files中没有ContentTypeRegExpFilter.

indexing search-engine web-crawler cxml heritrix

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

在xpath中连接两个字符串序列

如何在xpath 2.0中创建两个字符串序列的并集?

有这两个功能......

<xsl:function name="my:foo" as="xs:string*">
    ....
</xsl:function>

<xsl:function name="my:bar" as="xs:string*">
    ....
</xsl:function>
Run Code Online (Sandbox Code Playgroud)

我想迭代两个结果字符串序列,如:

<xsl:variable name="myResult" select="for $s in my:foo() ??union?? my:bar() return my:doSomethingWith($s)" />
Run Code Online (Sandbox Code Playgroud)

xslt xpath

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

元注释中的@ActiveProfiles和测试类不起作用

我创建了一个元注释@EmbeddedMongoDBUnitTest,它激活了两个配置文件,用于基于弹簧的单元测试.基本设置有效:

@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@ActiveProfiles({"embeddedMongoDB", "embeddedMongoDBUnitTest"})
public @interface EmbeddedMongoDBUnitTest {
}

@RunWith(SpringJUnit4ClassRunner.class)
@EmbeddedMongoDBUnitTest
@ContextConfiguration(...)
public class WorkingTest {
    ...
}
Run Code Online (Sandbox Code Playgroud)

现在,当尝试使用测试类本身上的另一个@ActiveProfiles注释激活另一个配置文件时,@ EmbeddedMongoDBUnitTest中的配置文件不再被激活:

@RunWith(SpringJUnit4ClassRunner.class)
@EmbeddedMongoDBUnitTest
@ActiveProfiles({"h2IntegrationTests"})
@ContextConfiguration(...)
public class NotWorkingTest {
    ...
}
Run Code Online (Sandbox Code Playgroud)

有没有理由说这不起作用,或者这是春季测试代码中的错误?

junit spring annotations spring-test spring-annotations

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

Thymeleaf:如何在表达式中使用fragment参数

有没有办法在表达式中使用片段参数?

我想创建一个片段来显示带有相应绑定错误的字段,例如:

<div th:fragment="alert (field, fieldLabel)">
    <label><span th:text="${fieldLabel}">Label:</span><input type="text" th:errorclass="field_error" th:field="*{field}"/></label>
    <div th:if="${#fields.hasErrors(field)}"><span th:errors="*{field}">Some error</span></div>
</div>
Run Code Online (Sandbox Code Playgroud)

获取片段:

<div th:replace=":: alert (field='firstName', fieldLabel='Firstname')">Field</div>
Run Code Online (Sandbox Code Playgroud)

如何在th:field和th:errors属性的表达式中使用field参数?*{field}至少不起作用.

spring spring-mvc thymeleaf

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

在Facebook个人资料中没有返回用户名

我刚刚在developers.facebook.com上重新注册了我们的facebook应用程序,获得了app id和secret.到现在为止还挺好.

现在,当我使用新的appId和appSecret获取用户配置文件时,用户名字段为空,其中像lastName等其他字段被设置(我UserProfile userProfile = connection.fetchUserProfile()使用spring-social-facebook).

我确定以前用旧的appId填写了用户名字段.我不知道为什么用户名现在不再由facebook发送了.如果用户登录Facebook,我们需要用户名来本地存储用户.

这是我缺少的Facebook应用程序设置页面上的设置吗?或者在Facebook方面做了一些改变?

facebook username facebook-graph-api spring-social spring-social-facebook

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