2015-09-24 127 views
0

在我的項目中,我使用swagger.this工作正常。我使用swagger版本0.5.0.i需要生成json格式的輸出,但招搖只產生xml format.how來重新解決這個錯誤。我使用spring和rest服務。Swagger返回xml格式,而不是json.I需要json格式來查看

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"; 
    } 

} 
+0

這是一段代碼。你是否縮小了問題發生的位置?我建議幫助我們通過[將示例代碼削減爲最小但完整的示例]來幫助您(https://stackoverflow.com/help/mcve)。 –

+0

pom.xml什麼是我使用的依賴關係.and spring xml如何配置權限掃描到我的java controller.and控制器定義我正在使用哪個控制器。上面的代碼非常簡單的示例 –

回答