Swagger返回xml格式而不是json.I需要json格式才能查看

时间:2015-09-24 03:18:22

标签: java json rest swagger

在我的项目中,我正在使用swagger.t这对我工作正常。我正在使用swagger版本0.5.0.i需要生成json格式的输出但swagger只生成xml format.how来重新发送此错误。我正在使用春季和休息服务。

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>csrfspring</groupId>
        <artifactId>csrfspring</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
        <build>
            <sourceDirectory>src</sourceDirectory>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <warSourceDirectory>WebContent</warSourceDirectory>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
            </plugins>
        </build>



        <properties>
            <jdk.version>1.6</jdk.version>
            <spring.version>4.2.1.RELEASE</spring.version>

            <jstl.version>1.2</jstl.version>
            <javax.servlet.version>3.1.0</javax.servlet.version>
        </properties>

        <dependencies>

            <dependency>
                <groupId>com.mangofactory</groupId>
                <artifactId>swagger-springmvc</artifactId>
                <version>0.5.0</version>
            </dependency>

            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.0.2</version>
            </dependency>
            <dependency>
                <groupId>javax.ws.rs</groupId>
                <artifactId>javax.ws.rs-api</artifactId>
                <version>2.0.1</version>
            </dependency>

            <dependency>
                <groupId>com.wordnik</groupId>
                <artifactId>swagger-core</artifactId>
                <version>1.0</version>
            </dependency>

            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>${javax.servlet.version}</version>
                <scope>provider</scope>
            </dependency>

            <!-- Spring 3 dependencies -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>




            <!-- jstl for jsp page -->
            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>${jstl.version}</version>
            </dependency>



            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-web-api</artifactId>
                <version>7.0</version> <!-- Put here the version of your Java EE app, in my case 7.0 -->
                <scope>provided</scope>
            </dependency>


        </dependencies>


    </project>

Spring xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">


    <mvc:default-servlet-handler />
    <mvc:annotation-driven />

   <context:component-scan base-package="com.examle.anand" />

<bean class="com.mangofactory.swagger.configuration.DocumentationConfig" />

<context:property-placeholder location="classpath:/swagger.properties" />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>

</beans>

controller in java
package com.examle.anand;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;

import com.wordnik.swagger.annotations.Api;

@Controller
@Api(value = "hello", description = "it simple test api") 
@RequestMapping("/hello")

public class HelloController{

   @RequestMapping(value="/test",method = RequestMethod.GET)
   public String printHello(ModelMap model) {

      System.out.println("TEsted i am inside controller");
      model.addAttribute("message", "Hello Spring MVC Framework!");

      return "hello";
   }

}

1 个答案:

答案 0 :(得分:0)

只需通过常见的XML库从XML创建Java对象,然后通过Jackson lib创建JSON。查看http://www.studytrails.com/java/json/jackson-create-json.jsp并阅读瑞士格规格http://swagger.io/specification/。但是你想要完整的JSON支持,所以使用RAML。