415 java jersey 中不支持的媒体类型 [英] 415 unsupported media type in java jersey

查看:19
本文介绍了415 java jersey 中不支持的媒体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的代码中遇到问题 415 unsupported MEDIA type 并且服务器拒绝了这个请求,因为请求实体的格式不支持发布方法.

I am having problem with below code saying 415 unsupported MEDIA type and The server refused this request because the request entity is in a format not supported for post method.

到处搜索,做了所有的事情,但没有结果.

Searched everywhere and did all the things yet no result.

救命!!

下面是主要代码

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

用户.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>

推荐答案

问题是你的方法参数类型 MultivaluedHashMap

The problem is your method parameter type MultivaluedHashMap

public Activity createActivityParams(MultivaluedHashMap<String, String> formse){

处理application/x-www-form-urlencodedMultivaluedMap的提供者,只支持MultivaluedMapMultivaluedMap 注入.您可以在 源代码

The provider that handle application/x-www-form-urlencoded and MultivaluedMap, only supports MultivaluedMap or MultivaluedMap<String, String> injections. You can see in the source code

@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));
}

所以只要把方法参数改成MultivaluedMap

So just change the method parameter to MultivaluedMap<String, String>

这篇关于415 java jersey 中不支持的媒体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆