3

xsd が指定されているプロジェクトがあります。xjc コンパイラを使用して Java クラスを生成します。次に、クラスに XmlRootElement 属性で注釈を付けます。AnnotationMethodHandlerAdapter で Jaxb2 マーシャリング/アンマーシャリング Bean を使用して sevlet を構成しました。名前空間なしで xml を送信すると、415 エラーが発生します。

ソースコードは次のファイルです - web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Test</display-name>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

ファイル - dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd"
    xmlns:oxm="http://www.springframework.org/schema/oxm">

    <context:component-scan base-package="com.test.endpoints" />

    <tx:annotation-driven />

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
          <list> 
            <ref bean="marshallingHttpMessageConverter"/>
          </list>
        </property>
    </bean>
    <bean id="marshallingHttpMessageConverter" 
          class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
           <constructor-arg ref="jaxb2Marshaller" />
    </bean> 
    <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="classesToBeBound">
            <list>
                <value>com.test.users.User</value>
                <value>com.test.users.Users</value>
            </list>
        </property>
    </bean>
    <!-- Should be defined last! -->
<!--    <mvc:annotation-driven />-->
</beans>

ファイル - user.xsd

    <element name="users">
        <complexType>
            <sequence>
                <element name="user" type="tns:user" minOccurs="0"
                    maxOccurs="unbounded" />
            </sequence>
        </complexType>
    </element>
    <complexType name="user">
        <sequence>
            <element name="id" type="int" />            
            <element name="email" type="string"></element>
            <element name="first_name" type="string"></element>
            <element name="last_name" type="string"></element>          
        </sequence>
    </complexType>
</schema>

このコマンドを使用して、上記の xsd の Java クラスを生成しました。

xjc -p com.test.users ..\xsd\user.xsd

このコマンドの出力は

parsing a schema...
compiling a schema...
com\test\users\ObjectFactory.java
com\test\users\User.java
com\test\users\Users.java
com\test\users\package-info.java

User.java に @XmlRootElement(name="user") のアノテーションを付けました。

ファイル - UserService.java

package com.test.endpoints;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.test.users.User;

@Controller
@RequestMapping("/users")
public class UserService {
    @RequestMapping(value="/new", method=RequestMethod.POST)
    @ResponseBody
    public User createUser(@RequestBody User user) {
        System.out.println(user.getFirstName());

        return user;
    }
}

このcurlコマンドでREST APIをテストしました

curl -X POST -HContent-type:application/xml -HAccept:application/xml  --data "<?xml version="1.0" encoding="UTF-8"?><user><id>1</id><email>email@email.com</email><first_name>first_name</first_name><last_name>last_name</last_name></user>" http://localhost:8080/Test/rest/users/new

出力は

The request sent by the client was syntactically incorrect ()

誰かが私が間違っているところを教えてください。

ありがとう

4

3 に答える 3

3

私も同じ問題に遭遇していました。私のControllerメソッドは次のようになりました。

@RequestMapping(method=RequestMethod.POST, value="/users/create",
        headers="Accept=application/xml")
public @ResponseBody User createUser(@RequestBody User user) {
    return user;
}

基本的に、POSTが正しく機能していることを確認するために、概念実証としてユーザーオブジェクトを返すだけでした。しかし、私は「構文的に正しくない」メッセージに出くわし続けました。コードを実際に更新して、次のような有効なUserオブジェクトを取得したら、次のようになります。

@RequestMapping(method=RequestMethod.POST, value="/users/create",
        headers="Accept=application/xml")
public @ResponseBody User createUser(@RequestBody User user) {
    userService.createUser(user);
    // do work
    User newUser = userService.getUserById(user.getId());
    return newUser;
}

正しく動作し始めました。

次のCURLコマンドを使用してRESTAPIをテストしました。

curl -X POST -HContent-type:application/xml -HAccept:application/xml --data "<user><username>Quinnster</username><password>password</password></user>" http://localhost:8080/api/users/create

別のAPIを使用してユーザーを一覧表示することで、新しいユーザーが適切に作成されたことを確認できました。

繰り返しますが、これがあなたが抱えている問題とまったく同じかどうかはわかりませんが、これで解決しました。

ありがとう、クイン

于 2011-07-11T22:26:20.033 に答える
1

クラスの @RequestMapping を次から変更してみてください。

@RequestMapping("/users")

に:

@RequestMapping("/users/*")

次に、メソッド宣言を次のように変更します。

@RequestMaqpping(method=RequestMethod.POST)
@ResponseBody
public void  create(@RequestBody User user) {

Springはメソッド名から URL の最後の部分を取得するため、URL はhttp://localhost:8080/rest/users/createになります。

于 2011-03-14T14:25:31.903 に答える
0

カールラインをに変更してみてください

curl -X POST -HContent-type:application/xml -HAccept:application/xml  \
     --data '<?xml version="1.0" encoding="UTF-8"?><user><id>1</id><email>email@email.com</email><first_name>first_name</first_name><last_name>last_name</last_name></user>' \
     http://localhost:8080/Test/rest/users/new

それらは XML 内の引用符であり、入力に干渉した可能性があります

于 2011-03-14T11:33:21.180 に答える