我有一个REST服务器,它在响应正文中发送JSON.我最近开始阅读Apache Camel.我使用以下命令将请求发送到我的REST服务.
from("direct:start").setHeader("token", simple("234da"))
.to("http://localhost:8088/foo/bar/?foo1=bar1");
Run Code Online (Sandbox Code Playgroud)
现在响应将是一个JSON,有没有什么方法我可以使用某种方法直接将这个JSON直接放入POJO to()(类似这样的东西)?
to("http://localhost:8088/foo/bar/?foo1=bar1").toPOJO();
Run Code Online (Sandbox Code Playgroud)
我更喜欢非Spring解决方案.
谢谢
我一直在nodejs中编写模块如下:
module.exports = function (logger, db, external,constants) {
return {
//something
}
}
Run Code Online (Sandbox Code Playgroud)
最近我的团队中有人建议整个脚本应该包含在一个函数中,以避免全局混淆变量,如下所示:
(function () {
'use strict';
module.exports = function (logger, db, external,constants) {
return {
//something
}
}
}());
Run Code Online (Sandbox Code Playgroud)
据我所知,这种做法通常用于客户端代码.但是在nodejs的服务器端这是必需的吗?我认为在nodejs中确实没有全局范围,只有module.exports是可以访问的,无论我们在脚本文件中编写什么(当然不要在这里疯狂).
我正在使用Apache CXF来创建一个简单的restful应用程序.我有一个客户端类,它将一个JSON对象发布到服务器,服务器在一些操作后返回一个JSON.但是当我执行代码时,我得到了
"org.apache.cxf.interceptor.Fault: .No message body writer has been found for class:
class org.codehaus.jettison.json.JSONObject, ContentType : application/json."
Run Code Online (Sandbox Code Playgroud)
我的客户代码:
public class Client {
public static void main(String[] args) {
try{
URI uri = new URI("http://localhost:8022/RestDemo");
WebClient client = WebClient.create(uri);
String ret = client.path("rest").path("server").path("welcome").accept(MediaType.TEXT_PLAIN).get(String.class);
System.out.println(ret);
JSONObject json = new JSONObject();
json.put("name", "ronaldo");
json = client.path("rest").path("server").path("op").type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(json, JSONObject.class);
System.out.println(json);
System.out.println(json.has("reverse")?json.getString("reverse"):"dont have");
}catch(Exception e){
System.out.println("e"+e.getLocalizedMessage());
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
请帮忙.
背景:
我有一个REST服务让我们说CustomerService现在有一个方法getCustomer(id, country).现在的要求是,根据国家/地区,我必须执行不同的业务逻辑,例如访问不同的数据库或一些自定义规则,然后报告我收到了这样的请求.
首先根据国家我使用Factory模式解决不同的实现,如下所示:
所有国家/地区实施的通用接口
public Interface CustomerServiceHandler{
Cusomer getCustomer(String id, String country);
}
Run Code Online (Sandbox Code Playgroud)
然后工厂作为
public class CustomerServiceHandlerFactory{
public CustomerServiceHandler getHandler(String country){...};
}
Run Code Online (Sandbox Code Playgroud)
使用Facade实现详细信息
请注意,此外观是从REST类调用的,即CustomerService
public CustomerServiceFacade{
public Customer getCustomer(String id, String country){
//use factory to get handler and then handler.getCustomer
Customer customer = factory.getHandler(country).getCustomer(id,country);
//report the request
reportingService.report('fetch-customer',....);
return customer;
}
}
Run Code Online (Sandbox Code Playgroud)
按照SRP(单一责任原则),这个立面并没有实现单一目标.它正在抓住客户以及报告收到此类请求.所以我想到装饰模式如下.
使用Decorator模式实现:
//this is called from Rest layer
public ReportingCustomerHandler implements CustomerServiceHandler{
//this delegate is basically the default implementation and has factory …Run Code Online (Sandbox Code Playgroud) 我对骆驼路线及其两个端点有点困惑:Direct 和 Seda。好吧,假设我有一条这样的路线:
public void configure()
{
from("direct:services")
.process(//Some processing here)
.to("http://ThirdPartyServers")
}
Run Code Online (Sandbox Code Playgroud)
除此之外,我有一个休息 Web 服务,它接收多个请求,进行一些处理,然后将消息移交给此路由以从某些第三方服务器获取响应。我已经通过 Spring 框架实例化了 Camel Context,如下所示:
<camelContext id="appCamelContext" xmlns="http://camel.apache.org/schema/spring"
trace="true" streamCache="true">
<propertyPlaceholder id="properties"
location="classpath:camel.properties" />
<camel:routeBuilder ref="oneRouteBuilder" />
<camel:routeBuilder ref="photosRouteBuilder" />
</camelContext>
Run Code Online (Sandbox Code Playgroud)
现在的问题是,我立即向该路由发送了多条不同的消息。现在Camel文档说直接组件在单线程中调用并且是同步的。那么所有消息会同时处理还是会逐一处理呢?
另外,如果我将直接组件更改为 seda,会有什么不同吗?
TIA
更新[在 Petter 的回答之后]:虽然 Petter 的回答已经澄清,但我对相同的 Direct 和 Seda 组件有新的疑问。可以说我的路线现在是这样的:
public void configure(){
from("direct:services")
.choice()
.when("some predicate here-Predicate1")
.to("seda:predicate1")
.otherwise()
.to("seda:fallback")
.end();
from("seda:predicate1")
.process("some processing")
.to("http://ThirdPartyServers");
from("seda:fallback")
.process("some processing")
.to("jms:fallbackqueue");
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我从不同线程向直接组件发送 5 条消息,那么这些消息将被并发处理。正如您在上面的路由中看到的,直接组件将消息发送到 seda 组件。那么现在 seda 组件将只有一个线程来处理所有不同的 5 条消息吗?意思是最后所有的消息都会被一一处理吗?
有没有办法只允许POST请求到j_security_check?我想拒绝GET.我正在使用基于表单的安全性,并希望只允许帖子到j_security_check.如果通过GET发出登录请求,则应拒绝该请求.
我是骆驼世界的新手,我在与jcr组件建立连接方面遇到了问题.看看我的测试代码:
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jcr.JcrConstants;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
public class ObjectToJCRRouteTest extends CamelTestSupport {
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:a")
.setProperty(JcrConstants.JCR_NODE_NAME,
constant("node"))
.setProperty("my.contents.property", body())
.to("jcr://admin:admin@localhost:8080/home/test");
}
};
}
@Test
public void putJCRNode() throws Exception {
template.sendBodyAndHeader("direct:a", null, null, null);
Thread.sleep(1000);
}
}
Run Code Online (Sandbox Code Playgroud)
我试过了jcr:// admin:admin @ localhost/home/test","jcr:// admin:admin @ localhost:8080/home/test","jcr:// admin:admin @ localhost:8080/repository /家/测试"
我使用rmi使用存储库资源管理器(http://www.subshell.com/en/toromiro/index.html)测试了jackrabbit 并且连接成功了
在任何帮助下提前感谢!
我的jackrabit服务器是:
jmartinez @ jmartinez:〜/ software/jackrabbit $ java -jar …
java ×5
apache-camel ×3
json ×2
cxfrs ×1
decorator ×1
facade ×1
jackrabbit ×1
java-ee ×1
javascript ×1
jcr ×1
node.js ×1
security ×1
spring ×1
weblogic ×1
websphere ×1