我在将 JSON 传递给 RestController 时遇到问题。它似乎没有消耗它。
Controller :
@PostMapping(path = "Users/{UserId}/Transactions",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public CompletableFuture<ResponseEntity<?>> startGameRound(@RequestBody TransactionRequest request,
@RequestParam("PartnerUserSessionKey") String sessionId,
@PathVariable("UserId") String playerUUID) throws ExecutionException, InterruptedException { // Logic }
TransactionRequest对象模型:
public class TransactionRequest {
@JsonProperty("TransactionType")
private TransactionType transactionType;
@JsonProperty("TransactionId")
private String transactionId;
@JsonProperty("TransactionCreationDate")
private LocalDateTime transactionCreationDate;
@JsonProperty("Amount")
private Long amount;
@JsonProperty("Rake")
private BigDecimal rake;
@JsonProperty("CurrencyCode")
private String currencyCode;
@JsonProperty("EntityReferences")
private List<EntityReference> entityReferences;
@JsonProperty("Game")
private Game game;
public TransactionRequest() {
}
//getters & setters & hashEquals & toString
}
下面是试图发送到 Controller 的测试方法:
def createTransactionRequest(Integer roundNum, String transType, BigDecimal transactionAmount) {
def transactionRequest = builder{
'TransactionType' transType
'TransactionId' "${new Random().nextInt(50)}"
'TransactionCreationDate' LocalDateTime.now().toString()
'Amount' transactionAmount.longValue()
'Rake' 0.0
'CurrencyCode' userCurrencyCode
'EntityReferences' builder.call([
{ 'EntityType' "CasinoRound"; 'EntityId' roundNum },
{ 'EntityType' "CasinoSession"; 'EntityId' gameSessionId }
])
'Game' (GameId: "111", GameName: "Some Game")
}
String currentDate = ZonedDateTime.now(ZoneId.of("UTC")).format(DATE_FORMATTER)
def authorizationHeader = buildAuthHeader("POST",
"Users/${player.uuid}/Transactions?PartnerUserSessionKey=$gameSessionId",
JsonOutput.toJson(transactionRequest), currentDate)
return client.post(path: "Users/${player.uuid}/Transactions", query: ["PartnerUserSessionKey": gameSessionId],
headers: ['Authorization' : authorizationHeader, DateUtc : currentDate]) {
type "application/json"
json transactionRequest
}
}
测试方法是用 Groovy 编写的。以及 groovy.json.JsonBuilder 中的生成器。 (忽略 authorizationHeader 方法,它只是用于生成授权签名,它有效)我试过只发送几个参数,但每次都会给我同样的错误。这是错误:
2017-09-05 12:38:26,005 WARN c.n.c.e.CommonExceptionHandler - Required request body is missing: public java.util.concurrent.CompletableFuture<org.springframework.http.ResponseEntity<?>> com.project.TransactionEndpoint.startGameRound(com.project.api.TransactionRequest,java.lang.String,java.lang.String) throws java.util.concurrent.ExecutionException,java.lang.InterruptedException
org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.util.concurrent.CompletableFuture<org.springframework.http.ResponseEntity<?>> com.project.endpoint.TransactionEndpoint.startGameRound(com.project.api.TransactionRequest,java.lang.String,java.lang.String) throws java.util.concurrent.ExecutionException,java.lang.InterruptedException
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:153)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:127)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
我相信它与对象模型有关,但不确定是什么。我试过传递不同的组合但没有运气。它只会给我 Required request body is missing。
这是生成的 JSON:
{"TransactionType":"CasinoRound_Stake","TransactionId":"31","TransactionCreationDate":"2017-09-05T15:38:08.610","Amount":400,"Rake":0.0,"CurrencyCode":"EUR","EntityReferences":[{"EntityType":"CasinoRound","EntityId":1},{"EntityType":"CasinoSession","EntityId":"9f31d8b9-28f7-4931-bb9d-73f90c2b2de7"}],"Game":{"GameId":"111","GameName":"Some Game"}}
最佳答案
您可能有一个过滤器,其中您已经阅读了正文。
关于java - Spring:缺少必需的请求正文。无法将 JSON 传递给 RestController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46055506/
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在使用Ruby2.1.1和Rails4.1.0.rc1。当执行railsc时,它被锁定了。使用Ctrl-C停止,我得到以下错误日志:~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`gets':Interruptfrom~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`verify_server_version'from~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.
我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些
我有一个非常简单的RubyRack服务器,例如:app=Proc.newdo|env|req=Rack::Request.new(env).paramspreq.inspect[200,{'Content-Type'=>'text/plain'},['Somebody']]endRack::Handler::Thin.run(app,:Port=>4001,:threaded=>true)每当我使用JSON对象向服务器发送POSTHTTP请求时:{"session":{"accountId":String,"callId":String,"from":Object,"headers":
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只