简体   繁体   English

Java Spring Restcontroller帖子

[英]Java Spring Restcontroller Post

I need help creating a POST request to my Java Spring Restcontroller. 我需要帮助来向Java Spring Restcontroller创建POST请求。

This is my controller - 这是我的控制器-

@RestController
@RequestMapping("hedgesimulator/")
public class HedgeSimulatorController {

    @RequestMapping(value = "hedgesim/", method = RequestMethod.POST)
    @ResponseBody
    public HedgeSimulatorLog putHedgeSimulation(
        @RequestBody HedgeSimulatorLog hedgeSimulatorLog) {

        System.out.println(hedgeSimulatorLog.toJsonString());
        return hedgeSimulatorLog;
    }
}

I am using Chrome's "Advanced Rest Client" Plugin to POST my request to my URL (I am sure my localhost is running properly, etc.) 我正在使用Chrome的“ Advanced Rest Client”插件将请求发布到我的URL(我确定本地主机运行正常,等等)

What do I need to add to my header? 我需要在标题中添加什么?

I receive an error for "HTTP 400 - Status report: The request sent by the client was syntactically incorrect" 我收到“ HTTP 400-状态报告:客户端发送的请求在语法上不正确”的错误

Please help! 请帮忙!

To pass an object to controller you must configure HttpMessageConverter which helds serialization and deserealization of this object. 要将对象传递给控制器​​,您必须配置HttpMessageConverter,该对象保留此对象的序列化和反序列化。 For example, if you want to pass an object to controller as JSON, set a MappingJackson2HttpMessageConverter as parameter in your mvc declaration in spring config file. 例如,如果要将对象作为JSON传递给控制器​​,请在spring config文件的mvc声明中将MappingJackson2HttpMessageConverter设置为参数。

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
    </mvc:message-converters>
</mvc:annotation-driven>

If http message converter configured properly, maybe request was improperly formed. 如果http消息转换器配置正确,则可能是请求的格式不正确。

@RequestMapping(value =“ / hedgesim /”,方法= RequestMethod.POST)

Try with following, hope you might resolve the issue: 请尝试以下操作,希望您可以解决此问题:

  1. Since you are using @RestController annotation, so no need to use @ResponseBody annotation again, which is redundant. 由于您正在使用@RestController批注,因此无需再次使用@ResponseBody批注,这是多余的。

  2. If you are using spring boot, then make sure you have added the below dependency . 如果您使用的是Spring Boot,请确保已添加以下依赖项。

      <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> 
  3. If the project is not spring boot, then add dependency for jackson : com.fasterxml.jackson.databind.ObjectMapper 如果项目不是春季启动,则为jackson添加依赖项: com.fasterxml.jackson.databind.ObjectMapper

  4. Just to make sure the request body is correct, what you can do is, execute request method first, get the JSON response, pass the same JSON for POST, so this might avoid some typo/human error while creating JSON data. 为了确保请求主体正确,您可以做的是,首先执行请求方法,获取JSON响应,为POST传递相同的JSON,因此在创建JSON数据时可以避免某些拼写错误/人为错误。

Hope, it helps. 希望能帮助到你。

You can do the following checks. 您可以进行以下检查。

  1. Validate the request body you are sending through some online tool like JSonLint . 通过一些在线工具(如JSonLint)验证要发送的请求正文。
  2. Check whether MappingJackson2HttpMessageConverter is registered or not. 检查是否注册了MappingJackson2HttpMessageConverter By default, Spring registers this converter if you have the jar in the classpath. 默认情况下,如果您在类路径中有jar,Spring将注册此转换器。
  3. No need to use @ResponseBody if you are using @RestController . 如果使用@ResponseBody则无需使用@RestController So remove it. 因此将其删除。

For a complete example on creating and consuming REST Service using Spring 4.0 , you can visit Techno Spots . 有关使用Spring 4.0创建和使用REST服务的完整示例,您可以访问Techno Spots

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM