java jersey中415不支持的媒体类型

时间:2016-03-17 12:24:15

标签: java rest jersey

我遇到以下代码的问题: 415不支持的MEDIA类型且服务器拒绝此请求,因为请求实体的格式不支持用于post方法。

随处搜索并做了所有事情但没有结果。

帮助!!

以下是主要代码

ActivityResource.java

@Path("activityresource")
public class ActivityResource {

    private ActivityRepo activityRepo=new ActivityRepositoryStub();

    @POST
    @Path("postActivity")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
    public Activity createActivityParams(MultivaluedHashMap<String, String> formse){
        System.out.println(formse.getFirst("discription"));
        System.out.println(formse.getFirst("duration"));

        Activity activity=new Activity();
        activity.setDiscription(formse.getFirst("discription"));
        activity.setDuration(Integer.parseInt(formse.getFirst("duration")));
        activityRepo.create(activity);

        return activity;
    }
}

Activity.java

package com.ws.model;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Activity {
    private String id;
    private String discription;
    private int duration;
    private User user;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    @XmlElement(name="desc")
    public String getDiscription() {
        return discription;
    }
    public void setDiscription(String discription) {
        this.discription = discription;
    }
    public int getDuration() {
        return duration;
    }
    public void setDuration(int duration) {
        this.duration = duration;
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
}

User.java

package com.ws.model;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class User {

    private String id;
    private String name;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

的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>com.ws.app</groupId>
    <artifactId>RestService</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <build>
        <finalName>RestServices</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <jersey.version>2.17</jersey.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <!-- NO VERSION NEEDED BECAUSE OF THE BOM -->
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
        </dependency>
    </dependencies>


</project>

WEB.XML

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
    <display-name>RestService</display-name>
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.ws.app</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>

1 个答案:

答案 0 :(得分:4)

问题是您的方法参数类型cargo build --verbose Updating registry `https://github.com/rust-lang/crates.io-index` error Unable to update registry https://github.com/rust-lang/crates.io-index Caused by: [2/-1] Could not open '.cargo/registry/index/github.com-88ac128001ac3a9a/bi/tt/bittorrent' for writing: Access is denied.

MultivaluedHashMap

处理public Activity createActivityParams(MultivaluedHashMap<String, String> formse){ application/x-www-form-urlencoded的提供商仅支持MultivaluedMapMultivaluedMap次注入。您可以在the source code

中看到
MultivaluedMap<String, String>

所以只需将方法参数更改为@Override public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { // Only allow types MultivaluedMap<String, String> and MultivaluedMap. return type == MultivaluedMap.class && (type == genericType || mapType.equals(genericType)); }

即可