小编che*_*1k4的帖子

保理尝试抓住

我有一个Java EE应用程序,其中有许多使用相同模式的Web服务:

public Response myWebService1() {
    try {
        // do something different depending on the web service called
    } catch (MyCustomException e1) {
        return Response.status(409).build();
    } catch (UnauthorizedException e2) {
        return Response.status(401).build();
    } catch (Exception e3) {
        return Response.status(500).build();
    }
}
Run Code Online (Sandbox Code Playgroud)

这可以分解这段代码吗?

java

23
推荐指数
3
解决办法
2948
查看次数

如何使用Spring通过websocket向客户端发送消息

我尝试使用带有websocket的Spring.我开始使用本教程进行调查.

在我的客户端,我有类似的东西来初始化与服务器的连接:

function connect() {
    var socket = new SockJS('/myphotos/form');
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function(frame) {
        setConnected(true);
        console.log('Connected: ' + frame);
        stompClient.subscribe('/topic/greetings', function(greeting){
            showGreeting(JSON.parse(greeting.body).content);
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

它很棒,在我的控制器中我可以在下面的类中完成我的过程:

@Controller
@RequestMapping("/")
public class PhotoController {

    @MessageMapping("/form")
    @SendTo("/topic/greetings")
    public Greeting validate(AddPhotosForm addPhotosForm) {
        return new Greeting("Hello world !");
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想做的是让一个线程向客户端发送消息,收听"/ topic/greeting".我写了这样的Runnable类:

public class FireGreeting implements Runnable {

    private PhotoController listener;

    public FireGreeting(PhotoController listener) {
        this.listener = listener;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep( …
Run Code Online (Sandbox Code Playgroud)

spring sockjs spring-websocket

20
推荐指数
2
解决办法
2万
查看次数

Maven jaxb2:xjc无法生成代码

我已将以下插件添加到pom.xml中的Maven构建中

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>xjc</goal>
            </goals>
            <configuration> 
                <extension>true</extension>                             
                <clearOutputDir>false</clearOutputDir>
                <schemaDirectory>${basedir}/src/main/resources/xsd</schemaDirectory>
                <schemaFiles>myapp.xsd</schemaFiles>                                                        
                <outputDirectory>${basedir}/src/main/java</outputDirectory>         
                <bindingDirectory>src/main/resources/xsd</bindingDirectory>     
                <bindingFiles>myapp-bindings.xjb</bindingFiles>
            </configuration>
        </execution>
    </executions>                       

</plugin>
Run Code Online (Sandbox Code Playgroud)

以下是构建错误.

[INFO] Ignored given or default xjbSources [C:\WorkSpace\MyApp\src\main\xjb], since it is not an existent file or directory.
[INFO] Ignored given or default sources [C:\WorkSpace\MyApp\src\main\xsd], since it is not an existent file or directory.
[WARNING] No XSD files found. Please check your plugin configuration.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.273s
[INFO] Finished at: Tue …
Run Code Online (Sandbox Code Playgroud)

build jaxb xjc maven

14
推荐指数
3
解决办法
4万
查看次数

Java HttpURLConnection - 使用Cookie进行POST

我正在尝试发送带有cookie的帖子请求.这是代码:

try {

    String query = URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode("value", "UTF-8");
    String cookies = "session_cookie=value";
    URL url = new URL("https://myweb");
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

    conn.setRequestProperty("Cookie", cookies);
    conn.setRequestMethod("POST");
    conn.setDoInput(true);
    conn.setDoOutput(true);

    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
    out.writeBytes(query);
    out.flush();
    out.close();

    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String decodedString;
    while ((decodedString = in.readLine()) != null) {
        System.out.println(decodedString);
    }
    in.close();

    // Send the request to the server
    //conn.connect();
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

问题是请求是在没有cookie的情况下发送的.如果我只做:conn.connect(); …

java cookies post httpurlconnection

11
推荐指数
1
解决办法
3万
查看次数

使用Java中的Rest API进行文件上载

我是REST API的新手.我想使用REST API将用户选择的文件上传到用户提供的路径(远程或本地路径).我的html文件有1个文本框和1个文件选择器.用户将在文本框中输入FilePath(本地或远程计算机文件夹位置).请提出如何解决此问题的建议.

这是我的代码:

FileUpload.html ::

<body>
    <form action="rest/file/upload" method="post" enctype="multipart/form-data">
        <p>
            Select a file : <input type="file" name="file" size="45" />
        </p>
        <p>Target Upload Path : <input type="text" name="path" /></p>
        <input type="submit" value="Upload It" />
   </form>
</body>
Run Code Online (Sandbox Code Playgroud)

UploadFileService.java

@Path("/file")
public class UploadFileService {

    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(
            @FormDataParam("file") InputStream uploadedInputStream,
            @FormDataParam("file") FormDataContentDisposition fileDetail,
            @FormParam("path") String path) {

        /*String uploadedFileLocation = "d://uploaded/"                                                                 + fileDetail.getFileName();*/

        /*String uploadedFileLocation = //10.217.14.88/Installables/uploaded/"                                                                  + fileDetail.getFileName();*/
        String uploadedFileLocation = path
                + fileDetail.getFileName();

        // save it …
Run Code Online (Sandbox Code Playgroud)

java rest web-services file-upload jersey

9
推荐指数
1
解决办法
7万
查看次数

是<?扩展...>方法参数的排他性?

假设我有两个类A,B并且扩展了A.用下面的方法我可以打印CollectionA或延长的东西A:

private static void print(Collection<? extends A> collection) {

    for (A element : collection) {
        System.out.println(element);
    }

}
Run Code Online (Sandbox Code Playgroud)

太棒了,我可以这样做:

public static void main(String[] args) {

    List<A> l1 = new ArrayList<>();
    l1.add(new A());
    l1.add(new B());
    print(l1);

    List<B> l2 = new ArrayList<>();
    l2.add(new B());
    l2.add(new B());
    print(l2);

}
Run Code Online (Sandbox Code Playgroud)

现在我的问题是为什么在我的方法main(或其他地方)我可以写这个

List<? extends A> l3 = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)

但不是这个

l3.add(new A());
l3.add(new B());
Run Code Online (Sandbox Code Playgroud)

我明白为什么我不能添加AB在中的实例l3.但为什么第一个似乎无用的声明被授权呢? …

java

9
推荐指数
2
解决办法
129
查看次数

Angular 2内联编辑器

我目前正在我的项目中使用带有打字稿的angular 2.我已经研究了angularjs的一些内联编辑器,并发现了 angular-xeditable.但是这个插件适用于angularjs 1.

是否有任何方法可以使用角2?或者另一种替代方案,例如x-editable for angular 2

我想简单的内联编辑器与编辑按钮.

PS我不希望js内联编辑器插件为此而不是角度的一部分(不是angularjs模块)

angular

9
推荐指数
1
解决办法
1万
查看次数

Wildfly自定义登录模块错误

我实现了一个通过Web服务访问的自定义loginModule,并检查JPA访问的数据库前面的用户名和密码.我在jboss 7.1上运行它并且它工作正常,但在将它移动到Wildfly(并添加我认为正确的配置)后,我得到了一个源自wildfly类内部的NullPointerException.有任何想法吗?

18:48:21,417 ERROR [io.undertow.request] (default task-3) UT005023: Exception handling request to /jass.ws/jaas/verifier/authenticateWithBasicUsernamePasswordAuth: java.lang.RuntimeException: java.lang.NullPointerException
    at org.wildfly.extension.undertow.security.JAASIdentityManagerImpl.verifyCredential(JAASIdentityManagerImpl.java:126)
    at org.wildfly.extension.undertow.security.JAASIdentityManagerImpl.verify(JAASIdentityManagerImpl.java:82)
    at io.undertow.security.impl.BasicAuthenticationMechanism.authenticate(BasicAuthenticationMechanism.java:110) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.security.impl.SecurityContextImpl$AuthAttempter.transition(SecurityContextImpl.java:281) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.security.impl.SecurityContextImpl$AuthAttempter.transition(SecurityContextImpl.java:298) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.security.impl.SecurityContextImpl$AuthAttempter.access$100(SecurityContextImpl.java:268) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.security.impl.SecurityContextImpl.attemptAuthentication(SecurityContextImpl.java:131) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.security.impl.SecurityContextImpl.authTransition(SecurityContextImpl.java:106) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.security.impl.SecurityContextImpl.authenticate(SecurityContextImpl.java:99) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.security.handlers.AuthenticationCallHandler.handleRequest(AuthenticationCallHandler.java:50) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.security.handlers.AuthenticationConstraintHandler.handleRequest(AuthenticationConstraintHandler.java:51) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.servlet.handlers.security.ServletSecurityConstraintHandler.handleRequest(ServletSecurityConstraintHandler.java:56) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227) …
Run Code Online (Sandbox Code Playgroud)

jaas wildfly

6
推荐指数
1
解决办法
6465
查看次数

写自定义Shiro境界

我正在构建自己的AuthorizingRealm子类,并且很难将它连接到我的子类SecurityManager.

我的境界的本质:

public class MyRealm extends AuthorizingRealm { 
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { 
        try { 
            // My custom logic here 

        } catch(Throwable t) { 
            System.out.println(t.getMessage()); 
        } 
        SimpleAuthenticationInfo authn = new SimpleAuthenticationInfo(new MyUser(), "somePassword");
        return authn;
    } 

    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { 
        try { 
            // My custom logic here 
        } catch(Throwable t) { 
            System.out.println(t.getMessage()); 
        }
        return new SimpleAuthorizationInfo();
    } 
}
Run Code Online (Sandbox Code Playgroud)

然后在我的'shiro.ini'中:

# ======================= 
# Shiro INI configuration 
# ======================= 
[main] 
myRealm = com.me.myapp.security.MyRealm 
Run Code Online (Sandbox Code Playgroud)

然后在我的Driver类/ main方法中(我用于测试): …

security authentication authorization shiro

6
推荐指数
1
解决办法
9112
查看次数

Eclipse使用错误的java home

我在Windows上使用Eclipse,我不明白它为什么使用特定的java版本.我可以使用Eclipse查看java版本,执行此帮助 - >关于Eclipse - >安装详细信息 - >配置.该java.home设置为C:\ Program Files文件\的Java\jre1.8.0_65.但我的环境变量配置如下:

  • JAVA_7_HOME:C:\ java\jdk1.7.0_79
  • JAVA_8_HOME:C:\ java\jdk1.8.0_60
  • JAVA_HOME:%JAVA_8_HOME%
  • 路径:...;%JAVA_HOME%\ bin; ...;

为什么Eclipse没有使用C:\ java\jdk1.8.0_60作为java.home.

java eclipse windows

4
推荐指数
2
解决办法
4422
查看次数