正在获取HTTP状态400-客户端发送的请求在语法上不正确:使用curl来发送/发送json请求

阿诺基

我正在使用带有两个xml / json对象的spring MVC,并且遇到以下错误:

HTTP Status 400 - The request sent by the client was syntactically incorrect ().

这是我的控制器。

@RequestMapping(method=RequestMethod.POST, value="/emp")
public @ResponseBody EmployeeList addEmp(@RequestBody Employee e) {
    employeeDS.add(e);
    List<Employee> employees = employeeDS.getAll();
    EmployeeList list = new EmployeeList(employees);
    return list;
}


@RequestMapping(method=RequestMethod.PUT, value="/emp/{id}")
public @ResponseBody EmployeeList updateEmp(@RequestBody Employee e, @PathVariable String id) {
    employeeDS.update(e);
    List<Employee> employees = employeeDS.getAll();
    EmployeeList list = new EmployeeList(employees);
    return list;
}

我正在尝试使用curl发送JSON对象:

curl -v -X PUT -HContent-type:application/json --data '{"id":3,"name":"guest","email":"[email protected]"}' http://localhost:8080/rest/service/emp/1


curl -v -X POST -HContent-type:application/json --data '{"id":3,"name":"guest","email":"[email protected]"}' http://localhost:8080/rest/service/emp

我在pom文件中添加了以下jackson依赖项:

<dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-asl</artifactId>
      <version>1.9.13</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-jaxrs</artifactId>
      <version>1.9.12</version>
    </dependency>

我还在servlet-dispatcher.xml中添加了以下配置:

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="order" value="1" />
    <property name="mediaTypes">
        <map>
            <entry key="xml" value="application/xml"/>
            <entry key="json" value="application/json" />

            <!--entry key="html" value="text/html"/-->

        </map>
    </property>
    <property name="defaultViews">
    <list>
      <!-- JSON View -->
      <bean
        class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
      </bean>

      <!-- JAXB XML View -->
      <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
        <constructor-arg>
            <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
               <property name="classesToBeBound">
                <list>
                   <value>com.mkyong.common.bean.Employee</value>
                   <value>com.mkyong.common.bean.EmployeeList</value>
                </list>
               </property>
            </bean>
        </constructor-arg>
      </bean>
     </list>
  </property>
  <property name="ignoreAcceptHeader" value="false" />

    <property name="viewResolvers">
        <list>
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="order" value="2" />
                <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
                <property name="prefix" value="/WEB-INF/pages/"/>
                <property name="suffix" value=".jsp"/>
            </bean>
        </list>
    </property>
</bean>


<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="marshallingConverter" />
            <ref bean="jsonConverter" />
        </list>
    </property>
</bean>

<bean id="marshallingConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
    <property name="marshaller" ref="jaxbMarshaller" />
    <property name="unmarshaller" ref="jaxbMarshaller" />       
    <property name="supportedMediaTypes" value="application/xml"/>
</bean>

<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="supportedMediaTypes" value="application/json" />
</bean>

这是Employee类的外观:

package com.mkyong.common.bean;

导入javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name =“ employee”)公共类员工{

private long id;
private String name;
private String email;

public Employee() {}

public Employee(long id, String name, String email) {
    this.id = id;
    this.name = name;
    this.email = email;
}

public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}

}

我的lib中有以下jar:

aopalliance-1.0.jar
aspectjrt-1.5.3.jar
aspectjweaver-1.5.3.jar
axiom-api-1.2.7.jar
commons-lang-2.5.jar
commons-logging-1.1.1.jar
jackson-core-asl-1.9.13.jar
jackson-jaxrs-1.9.12.jar
jackson-mapper-asl-1.9.13.jar
jaxb-api-2.1.jar
jaxb-impl-2.2.jar
joda-time-1.6.2.jar
opensaml-2.5.1-1.jar
openws-1.4.2-1.jar
slf4j-api-1.7.2.jar
spring-aop-3.1.3.RELEASE.jar
spring-beans-3.2.2.RELEASE.jar
spring-context-3.2.2.RELEASE.jar
spring-context-support-3.1.3.RELEASE.jar
spring-core-3.2.2.RELEASE.jar
spring-expression-3.2.2.RELEASE.jar
spring-jdbc-3.2.2.RELEASE.jar
spring-oxm-3.2.2.RELEASE.jar
spring-security-config-3.1.3.RELEASE.jar
spring-security-core-3.1.3.RELEASE.jar
spring-security-web-3.1.3.RELEASE.jar
spring-tx-3.1.3.RELEASE.jar
spring-web-3.2.2.RELEASE.jar
spring-webmvc-3.1.3.RELEASE.jar
spring-ws-core-2.1.2.RELEASE.jar
spring-ws-security-2.1.2.RELEASE.jar
spring-xml-2.1.2.RELEASE.jar
stax-api-1.0-2.jar
wsdl4j-1.6.1.jar
wss4j-1.6.5.jar
xmlsec-1.5.1.jar
xmlsec-2.0.jar
xmltooling-1.3.2-1.jar
xws-security-1.3.1.jar

我的GET适用于XML和JSON:

    @RequestMapping(method=RequestMethod.GET, value="/emp/{id}", headers="Accept=application/xml, application/json")
public @ResponseBody Employee getEmp(@PathVariable String id) {
    Employee e = employeeDS.get(Long.parseLong(id));
    return e;
}

用于以下curl命令:

curl -HAccept:application/xml  http://localhost:8080/rest/service/emp/1
curl -HAccept:application/json  http://localhost:8080/rest/service/emp/1
约翰

我实际上已经创建了您的项目。在Tomcat的Windows中使用Eclipse。我使用的是Spring 3.2.4(但我认为这没有什么不同)。

当我发送错误的json时遇到了与您相同的问题,因此我认为这是您的curl命令错误。你在窗户上吗?如果是这样,您必须转义报价。我发送了以下内容:

C:\> curl -v -X POST -HContent-type:application/json -d "{\"id\":3,\"name\":\"guest\",\"email\":\"[email protected]\"}" http://localhost:8080/HelperSpringMVC/emp
* Adding handle: conn: 0x6a3400
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x6a3400) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 8080 (#0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /HelperSpringMVC/emp HTTP/1.1
> User-Agent: curl/7.32.0
> Host: localhost:8080
> Accept: */*
> Content-type:application/json
> Content-Length: 47
>
* upload completely sent off: 47 out of 47 bytes
< HTTP/1.1 200 OK
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Wed, 11 Sep 2013 21:07:47 GMT
<
[{"id":10,"name":"john","email":"email"},{"id":3,"name":"guest","email":"[email protected]"}]
* Connection #0 to host localhost left intact

请注意,我的控制器在这里有点不同:

@Controller
public class controller {
@RequestMapping(method=RequestMethod.POST, value="/emp")
public @ResponseBody List<Employee> addEmp(@RequestBody Employee e, BindingResult results) {
    if (results.hasErrors()) {
        return new ArrayList<Employee>();
    }
    List<Employee> list = new ArrayList<Employee>();
    list.add(new Employee(10, "john", "email"));
    list.add(e);
    return list;
}
....

如果这无济于事或您想要我的任何文件,请大喊。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

春季HTTP错误400客户端发送的请求在语法上不正确

来自分类Dev

春季HTTP错误400客户端发送的请求在语法上不正确

来自分类Dev

Spring MVC复选框HTTP状态400客户端发送的请求在语法上不正确

来自分类Dev

DHTMLX dhtmlxForm.send()抛出HTTP状态400-“客户端发送的请求在语法上不正确。”

来自分类Dev

Spring HTTP Status 400-客户端发送的请求在语法上不正确(添加日期输入时)

来自分类Dev

在node.js中发送带有XML数据的POST请求:错误400,“客户端发送的请求在语法上不正确”

来自分类Dev

错误400评估表单“发送的请求在语法上不正确”

来自分类Dev

发送帖子请求时,客户端发送的请求在语法上不正确

来自分类Dev

客户端发送的请求在语法上不正确-JSON POST正文中的类型复杂

来自分类Dev

使用@DateTimeFormat,客户端发送的请求在语法上不正确

来自分类Dev

客户端发送的请求在语法上不正确。在春季使用@RequestParam

来自分类Dev

客户端发送的请求在语法上不正确。-Spring MVC + JDBC模板

来自分类Dev

Spring @RequestBody:客户端发送的请求在语法上不正确

来自分类Dev

收到错误:客户端发送的请求在语法上不正确

来自分类Dev

客户端发送的请求在spring mvc,ajax中在语法上不正确

来自分类Dev

客户端发送的请求在语法上不正确。在@ManyToOne关系中休眠,春季

来自分类Dev

“客户端发送的请求在语法上是不正确的。” -内容类型错误

来自分类Dev

Spring形式:客户端发送的请求在语法上不正确()

来自分类Dev

AngularJS发布数据显示“客户端发送的请求在语法上不正确”

来自分类Dev

使用脚本从客户端发送HTTP帖子并在NODEJS中获取请求

来自分类Dev

使用脚本从客户端发送HTTP帖子并在NODEJS中获取请求

来自分类Dev

从客户端发送HTTP请求后元素消失了吗?

来自分类Dev

使用Spring MVC进行CRUD时出现错误“客户端发送的请求在语法上不正确”

来自分类Dev

客户端发送的请求在语法上不正确,而使用AngularJS通过其余部分调用WebService时

来自分类Dev

在Win 8.1上使用Docker运行PhpStorm:客户端向HTTPS服务器发送了HTTP请求

来自分类Dev

如何使用Perl为HTTP请求发送不正确的Content-Length标头?

来自分类Dev

如何使用Apache HTTP客户端在GET请求中发送3d数组?

来自分类Dev

如何在iPhone编程中使用身份验证质询发送HTTP客户端请求

来自分类Dev

在使用 apache http 客户端发送之前修改请求 URI

Related 相关文章

  1. 1

    春季HTTP错误400客户端发送的请求在语法上不正确

  2. 2

    春季HTTP错误400客户端发送的请求在语法上不正确

  3. 3

    Spring MVC复选框HTTP状态400客户端发送的请求在语法上不正确

  4. 4

    DHTMLX dhtmlxForm.send()抛出HTTP状态400-“客户端发送的请求在语法上不正确。”

  5. 5

    Spring HTTP Status 400-客户端发送的请求在语法上不正确(添加日期输入时)

  6. 6

    在node.js中发送带有XML数据的POST请求:错误400,“客户端发送的请求在语法上不正确”

  7. 7

    错误400评估表单“发送的请求在语法上不正确”

  8. 8

    发送帖子请求时,客户端发送的请求在语法上不正确

  9. 9

    客户端发送的请求在语法上不正确-JSON POST正文中的类型复杂

  10. 10

    使用@DateTimeFormat,客户端发送的请求在语法上不正确

  11. 11

    客户端发送的请求在语法上不正确。在春季使用@RequestParam

  12. 12

    客户端发送的请求在语法上不正确。-Spring MVC + JDBC模板

  13. 13

    Spring @RequestBody:客户端发送的请求在语法上不正确

  14. 14

    收到错误:客户端发送的请求在语法上不正确

  15. 15

    客户端发送的请求在spring mvc,ajax中在语法上不正确

  16. 16

    客户端发送的请求在语法上不正确。在@ManyToOne关系中休眠,春季

  17. 17

    “客户端发送的请求在语法上是不正确的。” -内容类型错误

  18. 18

    Spring形式:客户端发送的请求在语法上不正确()

  19. 19

    AngularJS发布数据显示“客户端发送的请求在语法上不正确”

  20. 20

    使用脚本从客户端发送HTTP帖子并在NODEJS中获取请求

  21. 21

    使用脚本从客户端发送HTTP帖子并在NODEJS中获取请求

  22. 22

    从客户端发送HTTP请求后元素消失了吗?

  23. 23

    使用Spring MVC进行CRUD时出现错误“客户端发送的请求在语法上不正确”

  24. 24

    客户端发送的请求在语法上不正确,而使用AngularJS通过其余部分调用WebService时

  25. 25

    在Win 8.1上使用Docker运行PhpStorm:客户端向HTTPS服务器发送了HTTP请求

  26. 26

    如何使用Perl为HTTP请求发送不正确的Content-Length标头?

  27. 27

    如何使用Apache HTTP客户端在GET请求中发送3d数组?

  28. 28

    如何在iPhone编程中使用身份验证质询发送HTTP客户端请求

  29. 29

    在使用 apache http 客户端发送之前修改请求 URI

热门标签

归档